-
Notifications
You must be signed in to change notification settings - Fork 2
/
cube.py
108 lines (94 loc) · 4 KB
/
cube.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
from graph.vector import Vector
from vocabulary import GREEN, BLACK
from pygame.draw import polygon
class Cube:
def __init__(self, x0=0, y0=0, z0=0, color=GREEN, h0=1):
self.x = x0
self.y = y0
self.z = z0
self.color = color
self.h = h0
self.points = None
self.coords_2d = [[[(0, 0) for j in range(2)] for i in range(2)] for k in range(2)]
self.vizible = 1
self.main = Vector(x0, y0, z0)
self.set_coords_with_move()
def set_coords_with_move(self):
self.points = [[[Vector(0, 0, 0) for j in range(2)] for i in range(2)] for k in range(2)]
for i in range(2):
for j in range(2):
for k in range(2):
self.points[i][j][k].x = self.x + (-1) ** (i + 1) * self.h / 2
self.points[i][j][k].y = self.y + (-1) ** (j + 1) * self.h / 2
self.points[i][j][k].z = self.z + (-1) ** (k + 1) * self.h / 2
def print_all(self):
for i in range(2):
for j in range(2):
for k in range(2):
print(self.points[i][j][k].x)
print(self.points[i][j][k].y)
print(self.points[i][j][k].z)
def cub_are_vis_or(self, cam):
global counter
self.main.new_di_in_new_pos(cam)
self.main.set_coords_d_from_di()
if self.main.get_angle_cos(cam) > 1 / 2 and cam.d / 2 < self.main.d < 50:
self.vizible = 1
else:
self.vizible = 0
def draw_cube(self, screen, cam: Vector):
self.cub_are_vis_or(cam)
if self.vizible == 1:
for i in range(2):
for j in range(2):
for k in range(2):
x, y = self.points[i][j][k].get_vector(cam).coords_to_cam(cam)
self.coords_2d[i][j][k] = (x, y)
if cam.x > self.x + self.h / 2:
self.draw_square(screen, cam, i=3)
elif cam.x < self.x - self.h / 2:
self.draw_square(screen, cam, i=2)
if cam.y > self.y + self.h / 2:
self.draw_square(screen, cam, j=3)
elif cam.y < self.y - self.h / 2:
self.draw_square(screen, cam, j=2)
if cam.z > self.z + self.h / 2:
self.draw_square(screen, cam, k=3)
elif cam.z < self.z - self.h / 2:
self.draw_square(screen, cam, k=2)
else:
pass
def draw_square(self, screen, cam, i=0, j=0, k=0):
if i == 2 or i == 3:
polygon(screen, self.color,
[self.coords_2d[i - 2][0][0],
self.coords_2d[i - 2][0][1],
self.coords_2d[i - 2][1][1],
self.coords_2d[i - 2][1][0]])
polygon(screen, BLACK,
[self.coords_2d[i - 2][0][0],
self.coords_2d[i - 2][0][1],
self.coords_2d[i - 2][1][1],
self.coords_2d[i - 2][1][0]], 1)
if j == 2 or j == 3:
polygon(screen, self.color,
[self.coords_2d[0][j - 2][0],
self.coords_2d[0][j - 2][1],
self.coords_2d[1][j - 2][1],
self.coords_2d[1][j - 2][0]])
polygon(screen, BLACK,
[self.coords_2d[0][j - 2][0],
self.coords_2d[0][j - 2][1],
self.coords_2d[1][j - 2][1],
self.coords_2d[1][j - 2][0]], 1)
if k == 2 or k == 3:
polygon(screen, self.color,
[self.coords_2d[0][0][k - 2],
self.coords_2d[0][1][k - 2],
self.coords_2d[1][1][k - 2],
self.coords_2d[1][0][k - 2]])
polygon(screen, BLACK,
[self.coords_2d[0][0][k - 2],
self.coords_2d[0][1][k - 2],
self.coords_2d[1][1][k - 2],
self.coords_2d[1][0][k - 2]], 1)