-
Notifications
You must be signed in to change notification settings - Fork 0
/
figures.py
181 lines (149 loc) · 5.47 KB
/
figures.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
from math import pi, cos, sin
from numpy import dot, matrix
class Figure():
precise_depend = False
def __init__(self, name, verts, edges, center=(0, 0, 0)):
self.name = name
self.vertices = verts
self.edges = edges
self.center = center
def move(self, dx, dy, dz):
new_verts = []
for vert in self.vertices:
unlocked = list(vert)
unlocked[0] += dx
unlocked[1] += dy
unlocked[2] += dz
new_verts.append(tuple(unlocked))
self.vertices = tuple(new_verts)
new_center = []
new_center.append(self.center[0] + dx)
new_center.append(self.center[1] + dy)
new_center.append(self.center[2] + dz)
self.center = tuple(new_center)
def scale(self, kx, ky, kz):
new_verts = []
for vert in self.vertices:
v = [vert[i] - self.center[i] for i in range(len(vert))]
v[0] *= kx
v[1] *= ky
v[2] *= kz
final_pos = [v[i] + self.center[i] for i in range(len(v))]
new_verts.append(tuple(final_pos))
self.vertices = tuple(new_verts)
def rotate(self, ax, ay, az):
radian_x = ax * pi / 180
radian_y = ay * pi / 180
radian_z = az * pi / 180
sx, cx = sin(radian_x), cos(radian_x)
sy, cy = sin(radian_y), cos(radian_y)
sz, cz = sin(radian_z), cos(radian_z)
rot_x = matrix([[1, 0, 0], [0, cx, -sx], [0, sx, cx]])
rot_y = matrix([[cy, 0, sy], [0, 1, 0], [-sy, 0, cy]])
rot_z = matrix([[cz, -sz, 0], [sz, cz, 0], [0, 0, 1]])
new_verts = []
for vert in self.vertices:
v = [vert[i] - self.center[i] for i in range(len(vert))]
tr = matrix('{0}; {1}; {2}'.format(v[0], v[1], v[2]))
x_rot = rot_x.dot(tr)
y_rot = rot_y.dot(x_rot)
z_rot = rot_z.dot(y_rot)
listed = z_rot.tolist()
final = [listed[i][0] + self.center[i] for i in range(len(listed))]
new_verts.append(tuple(final))
self.vertices = tuple(new_verts)
class Plane(Figure):
precise_depend = True
def __init__(self, name, precision):
r = 10 * (2**(0.5))
step = 2 * pi / precision
angle = (step + pi) / 2
verts = []
edges = []
prev = None
for i in range(precision):
x, y = r * cos(angle), r * sin(angle)
verts.append((x, y, 0))
angle += step
if prev is not None:
edges.append((prev, i))
prev = i
edges.append((edges[len(edges) - 1][1], 0))
super().__init__(name, tuple(verts), tuple(edges))
class Ball(Figure):
precise_depend = True
def __init__(self, name, precision):
edges = []
verts = []
r = 10
z_step = pi / precision
raw_verts = []
verts.append((0, 0, r))
for layer in range(1, precision):
z = cos(z_step * layer) * r
layer_r = sin(z_step * layer) * r
layer_verts = []
step = pi / precision
angle = (step + pi) / 2
for i in range(precision * 2):
x, y = layer_r * cos(angle), layer_r * sin(angle)
layer_verts.append((x, y, z))
verts.append((x, y, z))
angle += step
raw_verts.append(layer_verts)
verts.append((0, 0, -r))
counter = 1
for j in range(1, 2 * precision * (precision - 1) + 1):
edges.append((max(0, j - 2 * precision), j))
for j in range((precision - 2) * 2 * precision + 1,
(precision - 1) * 2 * precision + 1):
edges.append((j, (precision - 1) * 2 * precision + 1))
for j in range(1, precision):
start = 1 + (j - 1) * 2 * precision
for i in range(2 * precision - 1):
edges.append((start + i, start + i + 1))
edges.append((start, start + 2 * precision - 1))
start += j * precision * 2
super().__init__(name, tuple(verts), tuple(edges))
class Cone(Plane):
precise_depend = True
def __init__(self, name, precision):
super().__init__(name, precision)
verts = list(self.vertices)
verts.append((0, 0, 20))
self.vertices = tuple(verts)
edges = list(self.edges)
amount = len(edges)
for i in range(amount):
edges.append((i, amount))
self.edges = tuple(edges)
class Pyramid(Cone):
precise_depend = False
def __init__(self, name):
super().__init__(name, 4)
class Tetrahedron(Cone):
precise_depend = False
def __init__(self, name):
super().__init__(name, 3)
class Cylinder(Plane):
precise_depend = True
def __init__(self, name, precision):
super().__init__(name, precision)
self.move(0, 0, -10)
self.center = (0, 0, 0)
circle = Plane('TEMP', precision)
circle.move(0, 0, 10)
verts = list(self.vertices)
edges = list(self.edges)
amount = len(self.vertices)
for i in range(amount):
verts.append(circle.vertices[i])
edges.append((i, i + amount))
edges.append((circle.edges[i][0] + amount,
circle.edges[i][1] + amount))
self.vertices = tuple(verts)
self.edges = tuple(edges)
class Cube(Cylinder):
precise_depend = False
def __init__(self, name):
super().__init__(name, 4)