-
Notifications
You must be signed in to change notification settings - Fork 1
/
dancing_quad_spiral.py
372 lines (304 loc) · 7.84 KB
/
dancing_quad_spiral.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import sys
try:
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
except:
print('Error: PyOpenGL not installed properly')
sys.exit( )
#import array
import signal
import math
#import random
##import OpenGL.GL as gl
##from OpenGL.GLUT import *
##from OpenGL.GLU import *
##from OpenGL.GL import *
def signal_handler(signal, frame):
print('u pressed ctrl-c')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
###signal.pause()
xrotspiral = 1.0
zrotspiral = 1.0
PI = 3.141592653
ngon=120
angle_step=2*PI/ngon
r1_step = 0.005
r2_step = 0.001
delta=0.6
theta1 = 2*PI/ngon
fullscreen = False
mouseDown = False
xrot = 0.2
yrot = 0.0
xdiff = 0.0
ydiff = 0.0
ndisc = 20
def spherical_to_cartesian3d(r,phi,theta):
x = r*math.sin(phi)*math.cos(theta)
y = r*math.sin(phi)*math.sin(theta)
z = r*math.cos(phi)
return (x,y,z)
def cartesian3d_to_spherical(x,y,z):
r = math.sqrt(x*x+y*y+z*z)
phi = math.acos(z/r)
theta = y/r/math.sin(phi)
return (r, phi, theta)
def init():
# glClearColor(0.93, 0.93, 0.93, 0.0)
glClearColor(1.0,1.0,1.0,0.0)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClearColor(0.0, 0.0, 0.0, 0.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glClearDepth(1.0)
### attempted but worst...
#glEnable(GL_BLEND)
#glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
#glEnable(GL_POINT_SMOOTH)
#glEnable(GL_LINE_SMOOTH)
#glEnable(GL_POLYGON_SMOOTH)
return True
Vertices = ((-1,-1,-1), (1,-1,-1),
(1,1,-1), (-1,1,-1),
(-1,-1,1), (1,-1,1),
(1,1,1), (-1,1,1))
Colors = ((0,0,0), (1,0,0),
(1,1,0), (0,1,0),
(0,0,1), (1,0,1),
(1,1,1), (0,1,1))
def planespiral(radius,ngon,turn,phi,theta,yc):
glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE)
glEnable(GL_BLEND)
glBegin(GL_LINE_STRIP)
#glColor3f(0.1, 0.2, 0.3)
theta = 2 * math.pi / ngon
rad1=radius/turn/ngon
rad=0.0
y=0.0
for i in range(ngon*turn):
glColor3fv(Colors[i%8])
x = rad * math.cos(i*theta)
z = rad * math.sin(i*theta)
y += yc
(x1,y1,z1) = point_rotatex(x,y,z, phi)
(x2,y2,z2) = point_rotatez(x1,y1,z1, theta)
rad += rad1
glVertex3fv((x2,y2,z2));
glEnd()
return
def planecircle(radius,ngon):
glBegin(GL_LINE_STRIP)
glColor3f(0.1, 0.2, 0.3)
theta = 2 * math.pi / ngon
for i in range(ngon):
x = radius * math.cos(i*theta)
z = radius * math.sin(i*theta)
y = 0.0
glVertex3fv((x,y,z));
glEnd()
return
def rotate_phi_theta(r, phi, theta, delta_r, delta_phi, delta_theta):
r = r+delta_r
phi = phi+delta_phi
theta = theta+delta_theta
new_vertices=[]
for v in range(len(vertices)):
(x,y,z) = vertices[v]
x *= math.sin(theta) * math.cos(alpha)
y *= math.cos(theta)
z *= math.sin(theta) * math.sin(alpha)
new_vertices.append((x,y,z))
return new_vertices
def rotate_theta_alpha(vertices, theta, alpha):
new_vertices=[]
for v in range(len(vertices)):
(x,y,z) = vertices[v]
x *= math.sin(theta) * math.cos(alpha)
y *= math.cos(theta)
z *= math.sin(theta) * math.sin(alpha)
new_vertices.append((x,y,z))
return new_vertices
def point_rotatex(x,y,z, angle):
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
y = y * cosa - z * sina
z = y * sina + z * cosa
return (x,y,z)
def plane_rotatex(vertices, angle):
""" Rotates the point around the X axis by the given angle in degrees. """
new_vertices=[]
for i in range(len(vertices)):
(x,y,z) = vertices[i]
(x,y,z)=point_rotatex(x,y,z,angle)
new_vertices.append((x,y,z))
return new_vertices
def point_rotatey(x,y,z, angle):
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
z = z * cosa - x * sina
x = z * sina + x * cosa
return (x,y,z)
def plane_rotatey(vertices, angle):
""" Rotates the point around the Y axis by the given angle in degrees. """
new_vertices=[]
for i in range(len(vertices)):
(x,y,z) = vertices[i]
(x,y,z)=point_rotatey(x,y,z,angle)
new_vertices.append((x,y,z))
return new_vertices
def point_rotatez(x,y,z,angle):
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
x = x * cosa - y * sina
y = x * sina + y * cosa
return (x,y,z)
def plane_rotatez(vertices, angle):
""" Rotates the point around the Z axis by the given angle in degrees. """
new_vertices=[]
for i in range(len(vertices)):
(x,y,z) = vertices[i]
(x,y,z)=point_rotatez(x,y,z,angle)
new_vertices.append([x,y,z])
return new_vertices
def face(a,b,c,d):
'''draw a face defined by four vertex indices'''
glBegin(GL_QUADS)
glColor3fv(Colors[a]); glVertex3fv(Vertices[a]);
glColor3fv(Colors[b]); glVertex3fv(Vertices[b]);
glColor3fv(Colors[c]); glVertex3fv(Vertices[c]);
glColor3fv(Colors[d]); glVertex3fv(Vertices[d]);
glEnd()
def colorcube():
'''Draws the entire cube, six faces'''
face(0,3,2,1)
face(2,3,7,6)
face(0,4,7,3)
face(1,2,6,5)
face(4,5,6,7)
face(0,1,5,4)
def edge(A, B):
'''Sends two vertices down the pipeline. Presumably they form an edge'''
glVertex3fv(Vertices[A]);
glVertex3fv(Vertices[B]);
def wireCube():
glBegin(GL_LINES)
## back
edge(0,1)
edge(1,2)
edge(2,3)
edge(3,0)
## front
edge(4,5)
edge(5,6)
edge(6,7)
edge(7,4)
## sides
edge(0,4)
edge(1,5)
edge(2,6)
edge(3,7)
glEnd()
def display():
global xrot, yrot
global ndisc
global xrotspiral, zrotspiral
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(
0.0, 0.0, 10.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0)
glRotatef(xrot, 1.0, 0.0, 0.0)
glRotatef(yrot, 0.0, 1.0, 0.0)
# glutWireCube(1)
#wireCube()
glColor3f(0.5, 0.0, 1.0)
## glutWireDodecahedron(2)
#colorcube()
#planespiral(2.0,36,10,45.0,45.0) #good
#planespiral(2.0,36,10,-45.0,0.0) #good
#bad
nos_turns = int(5*(math.sin(xrot)+4))
yc = 1.0/nos_turns/36
planespiral(1.0,36,nos_turns,-zrotspiral,xrotspiral,yc) #good
planespiral(2.0,36,nos_turns,xrotspiral, zrotspiral,yc*2) #bad
xrotspiral += 2
zrotspiral += 3
#planespiral(2.0,36,10,-45.0,-45.0) #good
#planespiral(2.0,36,10,45.0,45.0) #good
planespiral(2.0,36,nos_turns,-xrotspiral,2*zrotspiral,yc) #bad
planespiral(1.0,36,nos_turns,-xrotspiral,-zrotspiral,yc*4) #bad
#planespiral(2.0,36,10,180.0,0.0) #good
#planecircle(2.0,8)
glFlush()
glutSwapBuffers()
def resize(*args):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glViewport(0, 0, args[0], args[1])
gluPerspective(45.0, 1.0 * args[0] / args[0], 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def idle():
global xrot, yrot
global mouseDown
if not (mouseDown):
xrot += 0.3
yrot += 0.4
glutPostRedisplay()
def keyboard(*args):
##print args[0]
if (args[0]==27):
## print args[0]
sys.exit(1)
def specialKeyboard(*args):
##print args[0]
if (args[0] == GLUT_KEY_F1):
fullscreen = not(fullscreen)
if (fullscreen):
glutFullScreen()
else:
glutReshapeWindow(500, 500)
glutPositionWindow(50, 50)
def mouse(*args):
global xrot, yrot
global xdiff, ydiff
global mouseDown
if (args[0] == GLUT_LEFT_BUTTON and args[1] == GLUT_DOWN):
mouseDown = True
xdiff = args[2] - yrot
ydiff = -args[3] + xrot
else:
mouseDown = False
def mouseMotion(*args):
global xrot, yrot
global xdiff, ydiff
global mouseDown
if (mouseDown):
yrot = args[0] - xdiff
xrot = args[1] + ydiff
glutPostRedisplay()
def main():
glutInit(sys.argv)
glutInitWindowPosition(50, 50)
glutInitWindowSize(500, 500)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)
glutCreateWindow("Rotating Spiral")
glutDisplayFunc(display)
glutKeyboardFunc(keyboard)
glutSpecialFunc(specialKeyboard)
glutMouseFunc(mouse)
glutMotionFunc(mouseMotion)
glutReshapeFunc(resize)
if not (init()):
return 1
glutIdleFunc(idle)
glutMainLoop()
return 0
if __name__ == "__main__":
main()