-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmandelbrot.py
53 lines (45 loc) · 1.5 KB
/
mandelbrot.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
import png
import math, cmath
import sys
NAME = "mandelbrot.png"
WIDTH, HEIGHT = 1000, 1000
X, XL = -1, 3
Y, YL = 0, 3
MAXIT = 500 # Needs to be increased when zooming in, to get detail
sys.setrecursionlimit(1000) # the recursion limit needs to be increased if you make MAXIT higher
#Coordinates as center (normally they are top-left corner)
X -= XL/2
Y += YL/2
def mandelbrot(z, c, it):
if math.sqrt(z.real**2 + z.imag**2) > 2:
return it
elif it < MAXIT:
return mandelbrot(z**2+c, c, it+1)
else:
return None
def main():
image = []
for h in xrange(HEIGHT):
sys.stdout.write(str(float(h)/HEIGHT*100) + "% complete (line " + str(h) + " of " + str(HEIGHT) + ") \r")
sys.stdout.flush()
row = ()
for w in xrange(WIDTH):
x = X + w * float(XL)/WIDTH
y = Y - h * float(YL)/HEIGHT
m = mandelbrot(0, complex(x, y), 0)
if m == None:
row += (0,)
else:
m = m # could be changed to m = 2*m or something like that, when zoomin in, to get more detail
#Greyscale
row += (int((255.0/MAXIT*m)%255),)
#Color
#row = row + (math.floor(255/MAXIT*m), math.floor(255/MAXIT*m/5), 0,) #Change the equation to make fancy colors
image.append(row)
print ""
f = open(NAME, "wb")
w = png.Writer(WIDTH, HEIGHT, greyscale=True)
w.write(f, image)
f.close()
if __name__ == "__main__":
main()