forked from danyaal/mandelbrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.py
50 lines (40 loc) · 1.47 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
import numpy as np
import matplotlib.pyplot as plt
# counts the number of iterations until the function diverges or
# returns the iteration threshold that we check until
def countIterationsUntilDivergent(c, threshold):
z = complex(0, 0)
for iteration in range(threshold):
z = (z*z) + c
if abs(z) > 4:
break
pass
pass
return iteration
# takes the iteration limit before declaring function as convergent and
# takes the density of the atlas
# create atlas, plot mandelbrot set, display set
def mandelbrot(threshold, density):
# location and size of the atlas rectangle
realAxis = np.linspace(-2.25, 0.75, density)
imaginaryAxis = np.linspace(-1.5, 1.5, density)
# realAxis = np.linspace(-0.22, -0.219, 1000)
# imaginaryAxis = np.linspace(-0.70, -0.699, 1000)
realAxisLen = len(realAxis)
imaginaryAxisLen = len(imaginaryAxis)
# 2-D array to represent mandelbrot atlas
atlas = np.empty((realAxisLen, imaginaryAxisLen))
# color each point in the atlas depending on the iteration count
for ix in range(realAxisLen):
for iy in range(imaginaryAxisLen):
cx = realAxis[ix]
cy = imaginaryAxis[iy]
c = complex(cx, cy)
atlas[ix, iy] = countIterationsUntilDivergent(c, threshold)
pass
pass
# plot and display mandelbrot set
plt.imshow(atlas.T, interpolation="nearest")
plt.show()
# time to party!!
mandelbrot(120, 10000)