-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
209 lines (177 loc) · 6.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pygame
import datetime
import numpy as np
pygame.init()
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
d = 500
near = 0.1
cuboid_colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
]
vertices = [
[100, 100, 100], [150, 100, 100], [150, -50, 100], [100, -50, 100],
[100, 100, 200], [150, 100, 200], [150, -50, 200], [100, -50, 200],
[-50, 100, 100], [0, 100, 100], [0, -50, 100], [-50, -50, 100],
[-50, 100, 200], [0, 100, 200], [0, -50, 200], [-50, -50, 200],
[100, 100, 300], [150, 100, 300], [150, -50, 300], [100, -50, 300],
[100, 100, 400], [150, 100, 400], [150, -50, 400], [100, -50, 400],
[-50, 100, 300], [0, 100, 300], [0, -50, 300], [-50, -50, 300],
[-50, 100, 400], [0, 100, 400], [0, -50, 400], [-50, -50, 400]
]
edges = [
(0, 1), (1, 2), (2, 3), (3, 0),
(4, 5), (5, 6), (6, 7), (7, 4),
(0, 4), (1, 5), (2, 6), (3, 7),
(8, 9), (9, 10), (10, 11), (11, 8),
(12, 13), (13, 14), (14, 15), (15, 12),
(8, 12), (9, 13), (10, 14), (11, 15),
(16, 17), (17, 18), (18, 19), (19, 16),
(20, 21), (21, 22), (22, 23), (23, 20),
(16, 20), (17, 21), (18, 22), (19, 23),
(24, 25), (25, 26), (26, 27), (27, 24),
(28, 29), (29, 30), (30, 31), (31, 28),
(24, 28), (25, 29), (26, 30), (27, 31),
]
cuboids_edges = [
edges[0:12],
edges[12:24],
edges[24:36],
edges[36:48]
]
def interpolate(v0, v1, t):
return v0 + t * (v1 - v0)
def clip_edge(v0, v1):
t = (near - v0[2]) / (v1[2] - v0[2])
x = interpolate(v0[0], v1[0], t)
y = interpolate(v0[1], v1[1], t)
z = near
return [x, y, z]
def project(vertex):
x, y, z = vertex
if z < near:
return None
projection_matrix = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 1 / d, 0]
])
homogeneous_vertex = np.array([x, y, z, 1])
projected_homogeneous_vertex = projection_matrix.dot(homogeneous_vertex)
projected_vertex = projected_homogeneous_vertex / projected_homogeneous_vertex[3]
projected_x = int((projected_vertex[0] / projected_vertex[2]) * d + screen_width / 2)
projected_y = int((-projected_vertex[1] / projected_vertex[2]) * d + screen_height / 2)
return projected_x, projected_y
def translate(vertices, x_direction, y_direction, z_direction):
translation_matrix = np.array([
[1, 0, 0, x_direction],
[0, 1, 0, y_direction],
[0, 0, 1, z_direction],
[0, 0, 0, 1]
])
translated_vertices = []
for vertex in vertices:
homogenous_vertex = np.array(vertex + [1])
translated_vertex = translation_matrix.dot(homogenous_vertex)
translated_vertices.append(translated_vertex[:-1].tolist())
return translated_vertices
def rotate_x(vertices, angle):
cos_a, sin_a = np.cos(np.radians(angle)), np.sin(np.radians(angle))
rotation_matrix = np.array([
[1, 0, 0, 0],
[0, cos_a, -sin_a, 0],
[0, sin_a, cos_a, 0],
[0, 0, 0, 1]
])
rotated_vertices = []
for vertex in vertices:
homogenous_vertex = np.array(vertex + [1])
rotated_vertex = rotation_matrix.dot(homogenous_vertex)
rotated_vertices.append(rotated_vertex[:-1].tolist())
return rotated_vertices
def rotate_y(vertices, angle):
cos_a, sin_a = np.cos(np.radians(angle)), np.sin(np.radians(angle))
rotation_matrix = np.array([
[cos_a, 0, sin_a, 0],
[0, 1, 0, 0],
[-sin_a, 0, cos_a, 0],
[0, 0, 0, 1]
])
rotated_vertices = []
for vertex in vertices:
homogenous_vertex = np.array(vertex + [1])
rotated_vertex = rotation_matrix.dot(homogenous_vertex)
rotated_vertices.append(rotated_vertex[:-1].tolist())
return rotated_vertices
def rotate_z(vertices, angle):
cos_a, sin_a = np.cos(np.radians(angle)), np.sin(np.radians(angle))
rotation_matrix = np.array([
[cos_a, -sin_a, 0, 0],
[sin_a, cos_a, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
rotated_vertices = []
for vertex in vertices:
homogenous_vertex = np.array(vertex + [1])
rotated_vertex = rotation_matrix.dot(homogenous_vertex)
rotated_vertices.append(rotated_vertex[:-1].tolist())
return rotated_vertices
def draw():
screen.fill(BLACK)
for cuboid_index, cuboid_edges in enumerate(cuboids_edges):
color = cuboid_colors[cuboid_index % len(cuboid_colors)]
for edge in cuboid_edges:
v0, v1 = [vertices[i] for i in edge]
p0 = project(v0)
p1 = project(v1)
if v0[2] < near and v1[2] < near:
continue
if v0[2] < near:
v0 = clip_edge(v0, v1)
p0 = project(v0)
elif v1[2] < near:
v1 = clip_edge(v1, v0)
p1 = project(v1)
if p0 and p1:
pygame.draw.line(screen, color, p0, p1, 1)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
vertices = translate(vertices, 10, 0, 0)
elif event.key == pygame.K_RIGHT:
vertices = translate(vertices, -10, 0, 0)
elif event.key == pygame.K_UP:
vertices = translate(vertices, 0, -10, 0)
elif event.key == pygame.K_DOWN:
vertices = translate(vertices, 0, 10, 0)
elif event.key == pygame.K_q:
vertices = translate(vertices, 0, 0, -10)
elif event.key == pygame.K_e:
vertices = translate(vertices, 0, 0, 10)
elif event.key == pygame.K_x:
vertices = rotate_x(vertices, 5)
elif event.key == pygame.K_y:
vertices = rotate_y(vertices, 5)
elif event.key == pygame.K_z:
vertices = rotate_z(vertices, 5)
elif event.key == pygame.K_n:
d *= 0.9
elif event.key == pygame.K_m:
d *= 1.1
elif event.key == pygame.K_s:
filename = f"screenshot_{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.png"
pygame.image.save(screen, filename)
draw()
pygame.quit()