forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMandel.py
More file actions
160 lines (137 loc) · 3.52 KB
/
Copy pathPyMandel.py
File metadata and controls
160 lines (137 loc) · 3.52 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
# PyMandelBrot.py
# Plot a Mandelbrot set
# And include a mouse zoom
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
width = 400
height = 400
def zap():
global hcenter
global vcenter
global axrng
hcenter = 0.0
vcenter = 0.0
axrng = 2.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
# Set the screen plotting coordinates and the step
glClearColor(0.0, 0.0, 0.0, 0.0)
hstep = 2*axrng/(width)
vstep = 2*axrng/(height)
yinit = vcenter + axrng
xinit = hcenter - axrng
yfinal = vcenter - axrng
xfinal = hcenter + 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):
# Allows us to quit by pressing 'Esc' or 'q'
if key == chr(27):
sys.exit()
if key == "z":
zap()
if key == "q":
sys.exit()
def drawmandel():
glClear(GL_COLOR_BUFFER_BIT)
y = yinit
while y > yfinal:
y -= vstep
x = xinit
while x < xfinal:
x += hstep
n = 0
z = a = complex(x,y)
glBegin(GL_POINTS)
# n < 200 is the number of iterations
# Increase this value to show finer detail
# However finer detail results in slower execution
while n < 250:
n+=1
z = cos(z) + a
#print z
#z = log(sqrt(z.real * z.real + z.imag * z.imag)+1)
zz = abs(z)
# zz > 2 is the critical escape value
# Some functions require larger escape values
# This zz > 2 conditional provides coloration for # points outside the M-Set set
if zz > 40:
# Weird colors around the M-Set
#if abs(z.real) < 0.01 and abs(z.imag) < 0.01:
glColor3ub(8*zz,4*zz,6*zz)
glVertex2f(x,y)
n = 5001
# This zz < 2 conditional provides coloration for
# points inside the M-Set.
if zz < 40:
# Coloration in the M-Set
#glColor3f(3/sin(3*zz),cos(3*z.real),2*sin(zz))
#glColor3f(0.9, 0.2, 0.5)
#glVertex2f(x,y)
pass
glEnd()
def mouse(button, state, x, y):
global hcenter
global vcenter
global axrng
# 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:
axrng = axrng/2
if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
axrng = 2*axrng
if state == GLUT_DOWN:
hcenter = xinit + (xfinal - xinit)*x/width
vcenter = yinit + (yfinal - yinit)*y/height
print hcenter, vcenter
init()
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()
# End Program