forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyMandBound.py
111 lines (92 loc) · 2.39 KB
/
PyMandBound.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
# PyBoundMSet.py
# Plot an M-Set
# Using boundary scanning
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from random import *
from numpy import *
from time import *
import sys
# If psyco isn't installed, delete the next two lines!
import psyco
psyco.full()
axrng = 2.0
width = 400
height = 400
hstep = 2.0*axrng/width
vstep = 2.0*axrng/height
def init():
glClearColor(0.0, 0.0, 0.0, 0.0)
gluOrtho2D(-axrng,axrng,-axrng,axrng)
def drawboundmandel():
global escape
glClear(GL_COLOR_BUFFER_BIT)
# for a timer!
tim = time()
# Chooses the number of random pixels to check
# Increase this number for a more dense plot.
for i in range(0,100000):
# limits the M-Set ranges
# to speed up execution
# and choose random pixels in
# this more limited range
x = -2*axrng*random()+.5
y = 1.25*axrng*random()-1.25
# draws a triangle around the M-Set
# So we have fewer points to choose from
if y < .625*x + 1.25 and y > -.625*x - 1.25:
bound = 0
# check pixels at North, South,
# East, and West locations to see
# if these points escape to infinity
# If so, add 1 to bound variable
leng = escapetime(x+hstep,y)
if leng < 2:
bound += 1
leng = escapetime(x-hstep,y)
if leng < 2:
bound += 1
leng = escapetime(x,y-vstep)
if leng < 2:
bound += 1
leng = escapetime(x,y+vstep)
if leng < 2:
bound += 1
glBegin(GL_POINTS)
# If any, but not ALL neighboring
# pixels escape, then the current pixel
# is on the border of the M-Set, so plot it!
#
# Change second bound to bound < 5 for
# an interesting effect!
if bound > 0 and bound < 4:
glColor3ub(85*bound,50*bound,90*bound)
glVertex2f(x,y)
glEnd()
glFlush()
# Print the elapsed time in the console window
print time() - tim
def escapetime(x,y):
n = 0
z = a = complex(x,y)
# Low escape time for a quick plot
while n < 25:
# M-Set equation
z = z**2 + a
zz = abs(z)
# escape distance
if zz > 2:
n = 5001
n += 1
return zz
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)
glutInitWindowPosition(50, 50)
glutInitWindowSize(width, height)
glutCreateWindow("Julia Set")
glutDisplayFunc(drawboundmandel)
init()
glutMainLoop()
main()