forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMouseMandel.py
More file actions
345 lines (265 loc) · 7.17 KB
/
Copy pathPyMouseMandel.py
File metadata and controls
345 lines (265 loc) · 7.17 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
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
# PyMandelJulia.py
# Plot a Mandelbrot set
# And include a mouse zoom with
# Julia Set option enabled
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from numpy 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 axrng
global hstep
global vstep
global yinit
global xinit
# The following globals allow for the
# Julia Set options
global mandel
global tmprng
global julhcenter
global julvcenter
global julflag
global julx
global july
width = 400
height = 400
# mandel = 1 for M-Set
# mandel = 0 for Julia Set
# Start with the M-Set
mandel = 1
def zap():
# Reset everything
global hcenter
global vcenter
global axrng
global mandel
global julflag
hcenter = 0.0
vcenter = 0.0
axrng = 2.0
mandel = 1
julflag = 0
init()
def init():
# Identify the globals
global hcenter
global vcenter
global axrng
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)
# Dividing by (width+1) and (height+1) delays
# the onset of screen glitches or artifacts when
# zooming by making the hStep and vStep slightly smaller
hstep = 2*axrng/(width+1)
vstep = 2*axrng/(height+1)
# if mandel == 1 then we are plotting the M-Set
if mandel == 1:
yinit = vcenter + axrng
xinit = hcenter - axrng
yfinal = vcenter - axrng
xfinal = hcenter + axrng
else:
# if mandel <> 1 then we plot Julia
# the Julia Set uses different
# global variables so we don't forget
# the M-Set parameters
yinit = julvcenter + axrng
xinit = julhcenter - axrng
yfinal = julvcenter - axrng
xfinal = julhcenter + axrng
# 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)
# Set the matrix for the object we are drawing
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glutPostRedisplay()
def keyboard(key, x, y):
global mandel
global tmprng
global axrng
global hcenter
global vcenter
global julflag
if mandel == 1:
# We are working with the M-Set
# Store the M-Set axrng zoom factor
# So that we can restore it later if needed
tmprng = axrng
# Allows us to quit by pressing 'Esc' or 'q'
if key == chr(27):
sys.exit()
if key == "z":
zap()
if key == "j":
# Toggle the Julia Set
# and set axrng to original zoom
mandel = 0
axrng = 2.0
if key == "m":
# Toggle M-Set and restore the last
# M-Set axrng value so the M-Set looks
# the same as it did when we left it
mandel = 1
axrng = 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
# julX and julY contain the Julia Set
# seed coordinates
if mandel == 0:
a = complex(julx, july)
while y > yfinal:
y-= vstep
x = xinit
while x < xfinal:
x+= hstep
n = 0
# Choose M-Set or Julia Set
# If Julia Set is toggled, "a" already contains
# the complex number seed
if mandel == 1:
z = a = complex(x,y)
else:
z = complex(x,y)
glBegin(GL_POINTS)
# Escape time... increase this value above 25
# for a more detailed plot. Decrease
# this value for more speed.
while n < 25:
n+=1
z = z**2 + a
zz = abs(z)
# This is the escape distance. For some
# M-Set/Julia Sets such as sin() or exp() you
# may need to set this value higher than 2
# 50 works well for sin() functions
if zz > 2:
# Weird colors outside the M-Set
#glColor3f(3*sin(3*z.real),cos(3*z.real),4*cos(zz))
#glVertex2f(x,y)
n = 5001
# The same goes for this zz < 2 statement as above
if zz < 2:
# Coloration inside the M-Set
glColor3f(3*sin(3*zz),cos(3*z.real),2*sin(zz))
glVertex2f(x,y)
glEnd()
glFlush()
def mouse(button, state, x, y):
global hcenter
global vcenter
global axrng
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
axrng = axrng/2
init()
else:
# We use different center point variables here
# to keep the Julia Set and M-Set calculations
# separate
julhcenter = xinit + (xfinal - xinit)*x/width
julvcenter = yinit + (yfinal - yinit)*y/height
# We use a flag variable here so that the first
# Julia Set plot is normal size regardless of the
# Zoom factor on the M-Set. We don't want to
# cause a zoom on the first Julia Set
if julflag == 0:
# Print the value of the Julia Set seed
print "Julia", julhcenter, julvcenter
# Store the pixel coordinates in julX and julY
# for the Julia Set seed
julx = julhcenter
july = julvcenter
# Set the following variables to zero
# so the first Julia Set is centered in
# the graphics display window
julhcenter = 0.0
julvcenter = 0.0
# Show the Julia Set!
init()
else:
# NOW we can zoom on the Julia Set
julhcenter = xinit + (xfinal - xinit)*x/width
julvcenter = yinit + (yfinal - yinit)*y/height
axrng = axrng/2
init()
# Set the flag so subsequent mouse clicks zoom
# into the Julia Set
julflag = 1
if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
# This section is similar to the previous
# Section except that here we zoom out!
if mandel == 1:
hcenter = xinit + (xfinal - xinit)*x/width
vcenter = yinit + (yfinal - yinit)*y/height
axrng = 2*axrng
init()
else:
julhcenter = xinit + (xfinal - xinit)*x/width
julvcenter = yinit + (yfinal - yinit)*y/height
# Again, we don't want to initially zoom into
# the Julia Set... we want a "normal" Julia First
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
axrng = 2*axrng
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()