forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyMandelMouse.py
252 lines (227 loc) · 5.7 KB
/
PyMandelMouse.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
# PyMandelBrot.py
# Plot a Mandelbrot set
# And include a mouse zoom
# Julia Set option enabled
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from math import *
from time import *
import sys
# If psyco isn't installed, delete the next two lines!
import psyco
psyco.full()
# Set initial window width and height
# Declare global variables
global width
global height
global hCenter
global vCenter
global nRange
global hStep
global vStep
global yInit
global xInit
# The next lines allow for the
# Julia Set options
global mandel
global tmpRng
global julHCenter
global julVCenter
global julFlag
global julX
global julY
width = 300
height = 300
# mandel = 1 for M-Set
# mandel = 1 for Julia Set
mandel = 1
def zap():
# Reset everything
global hCenter
global vCenter
global nRange
global mandel
global julFlag
hCenter = 0.0
vCenter = 0.0
nRange = 1.5
mandel = 1
julFlag = 0
init()
def init():
# Identify the globals
global hCenter
global vCenter
global nRange
global hStep
global vStep
global yInit
global xInit
global yFinal
global xFinal
global mandel
global julHCenter
global julVCenter
# Set the screen plotting coordinates and the step
glClearColor(0.0, 0.0, 0.0, 0.0)
hStep = 2*nRange/(width+1)
vStep = 2*nRange/(height+1)
if mandel == 1:
yInit = vCenter + nRange
xInit = hCenter - nRange
yFinal = vCenter - nRange
xFinal = hCenter + nRange
else:
yInit = julVCenter + nRange
xInit = julHCenter - nRange
yFinal = julVCenter - nRange
xFinal = julHCenter + nRange
# Fill the entire graphics window!
glViewport(0, 0, width, height)
# Set the projection matrix... our "view"
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
# Set the window plot coordinates
#gluOrtho2D(xInit,xFinal,yFinal,yInit)
gluOrtho2D(xInit,xFinal,yFinal,yInit)
# Set the matrix for the object we are drawing
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glutPostRedisplay()
def keyboard(key, x, y):
global mandel
global tmpRng
global nRange
global hCenter
global vCenter
global julFlag
if mandel == 1:
tmpRng = nRange
# Allows us to quit by pressing 'Esc' or 'q'
if key == chr(27):
sys.exit()
if key == "z":
zap()
if key == "j":
mandel = 0
nRange = 2.0
if key == "m":
mandel = 1
nRange = tmpRng
julFlag = 0
init()
if key == "q":
sys.exit()
def drawMandel():
glClear(GL_COLOR_BUFFER_BIT)
y = yInit
# toggle Julia Set or M-Set
# mandel == 0 is the Julia Set
if mandel == 0:
a = complex(julX, julY)
glBegin(GL_POINTS)
tim = time()
while y > yFinal:
y-= vStep
x = xInit
while x < xFinal:
x+= hStep
n = 0
# Choose M-Set or Julia Set
if mandel == 1:
z = a = complex(x,y)
else:
z = complex(x,y)
while n < 200:
n+=1
try:
z = z*z + a
#z = sinh(z*z) + a
#z = tanh(z*z*z) + a
#z = sin(z)**2/(abs(z)-1)
except:
pass
zz = abs(z)
if zz > 2:
# Weird colors around the M-Set
glColor3f((3*z.real),(3*z.real),4*(zz))
glVertex2f(x,y)
n = 5001
if zz < 2:
# Coloration in the M-Set
#glColor3f(z.real*tan(9)+1,3*sin(20*zz)*cos(abs(z.imag)),2*cos(zz)*sin(40*zz)/sin(z.real))
glColor3f(3*(3*zz),(3*z.real),2*(zz))
#glColor3f(.9,.2,.5)
glVertex2f(x,y)
glEnd()
glFlush()
print time()-tim
def mouse(button, state, x, y):
global hCenter
global vCenter
global nRange
global julHCenter
global julVCenter
global julFlag
global julX
global julY
# Detect the left/right mouse buttons and the click
# Followed by resetting the origin
# Left mouse button zooms in, right button zooms out
if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
if mandel == 1:
hCenter = xInit + (xFinal - xInit)*x/width
vCenter = yInit + (yFinal - yInit)*y/height
nRange = nRange/2
init()
else:
julHCenter = xInit + (xFinal - xInit)*x/width
julVCenter = yInit + (yFinal - yInit)*y/height
if julFlag == 0:
print "Julia", julHCenter, julVCenter
julX = julHCenter
julY = julVCenter
julHCenter = 0.0
julVCenter = 0.0
init()
else:
julHCenter = xInit + (xFinal - xInit)*x/width
julVCenter = yInit + (yFinal - yInit)*y/height
nRange = nRange/2
init()
julFlag = 1
if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
if mandel == 1:
hCenter = xInit + (xFinal - xInit)*x/width
vCenter = yInit + (yFinal - yInit)*y/height
nRange = 2*nRange
init()
else:
julHCenter = xInit + (xFinal - xInit)*x/width
julVCenter = yInit + (yFinal - yInit)*y/height
if julFlag == 0:
print "Julia", julHCenter, julVCenter
julX = julHCenter
julY = julVCenter
julHCenter = 0.0
julVCenter = 0.0
init()
else:
julHCenter = xInit + (xFinal - xInit)*x/width
julVCenter = yInit + (yFinal - yInit)*y/height
nRange = 2*nRange
init()
julFlag = 1
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(50, 50)
glutInitWindowSize(width, height)
glutCreateWindow("Mandelbrot Set")
glutDisplayFunc(drawMandel)
glutMouseFunc(mouse)
glutKeyboardFunc(keyboard)
zap()
glutMainLoop()
main()