forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyQuat.py
More file actions
217 lines (178 loc) · 3.65 KB
/
Copy pathPyQuat.py
File metadata and controls
217 lines (178 loc) · 3.65 KB
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
# pyQuat.py
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import *
from random import *
import sys
#define some globals
global vv
global aff
global wd
global ht
global MouseX
global MouseY
global count
# variable to store the display list
global ptcloud
#define the vertex points
vv = []
#define the affine identity matrix
aff = (1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0)
#initial window and mouse settings
wd = 400
ht = 400
MouseX = wd/2
MouseY = ht/2
# calculate the quaternion fractal
def calcit():
global vv
global count
count = 1
s = 1
pi = 3.141592653588
a = 0.1
b = -0.75
theta = 20.
theta = 2*pi*theta/360.
x = a
y = b*cos(theta)
z = b*sin(theta)
# change the values of k and l to change the shape
k = 0.2
l = 0.7
phi = 20.
phi = 2*pi*phi/360.
a = k
b = l*cos(phi)
c = l*sin(phi)
while count < 20000:
x = x + a
y = y + b
z = z + c
r = sqrt(x*x + y*y + z*z)
x = x/r
y = y/r
z = z/r
r = sqrt(r)
m = sqrt(y*y + z*z)
e = sqrt(.5*(1+x))
g = sqrt(.5*(1-x))
if m == 0:
x = r
y = 0
z = 0
else:
x = r*e
y = r*y*g/m
z = r*z*g/m
count = count + 1
if random() > 0.5:
s = -s
x = s*x
y = s*y
z = s*z
if count > 30:
vv = vv + [(x,y,z)]
vv = vv + [(-x,-y,-z)]
dolist()
def dolist():
global ptcloud
# start storing the display list in ptcloud
ptcloud = glGenLists(1)
# compile the ptcloud points
glNewList(ptcloud, GL_COMPILE)
glPointSize(3.0)
glBegin(GL_POINTS)
for n in range(1,2*count-60,3):
glColor3f(sin(n),cos(n),sin(n)*cos(n))
glVertex3fv(vv[n])
glEnd()
glEndList()
def display():
global vv
global count
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glMultMatrixf(aff)
glCallList(ptcloud)
glPopMatrix()
glFlush()
glutSwapBuffers()
def keyboard(key, x, y):
if key == chr(27) or key == 'q':
sys.exit(0)
glutPostRedisplay()
#if we change the screen dimensions
def reshape(width, height):
global wd
global ht
glClearColor(0.0, 0.0, 0.0, 0.0)
if height == 0:
height = 1
wd = width
ht = height
glViewport(0,0,wd,ht)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if wd<=ht:
glOrtho(-2.0,2.0,-2.0*ht/wd,2.0*ht/wd,-2.0,2.0)
else:
glOrtho(-2.0*wd/ht,2.0*wd/ht,-2.0,2.0,-2.0,2.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
#does nothing at this point
#def motion():
# return 0
def chaptrack():
global MouseX
global MouseY
global wd
global ht
global aff
dx = (MouseX-wd/2)/128.0
dy = (MouseY-ht/2)/128.0
glMatrixMode(GL_TEXTURE)
glPushMatrix()
glLoadIdentity()
glRotatef(dx,0,1.0,0.0)
glRotatef(dy,1.0,0.0,0.0)
glMultMatrixf(aff)
aff = glGetFloatv(GL_TEXTURE_MATRIX)
glPopMatrix()
def idle():
chaptrack()
glutPostRedisplay()
def mousemotion(x,y):
global MouseX
global MouseY
MouseX = x
MouseY = y
def init():
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH)
def main() :
global wd
global ht
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE)
glutInitWindowPosition(50, 50)
glutInitWindowSize(wd, ht)
glutInit([])
glutCreateWindow("illiOct")
glutKeyboardFunc(keyboard)
glutReshapeFunc(reshape)
glutDisplayFunc(display)
#glutMotionFunc(motion)
#glutMouseFunc(mouse)
glutIdleFunc(idle)
glutPassiveMotionFunc(mousemotion)
init()
# calculate the fractal
calcit()
glutMainLoop()
main()