|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | + |
| 4 | +try: |
| 5 | + # Python 2 |
| 6 | + xrange |
| 7 | +except NameError: |
| 8 | + # Python 3 |
| 9 | + xrange = range |
| 10 | + |
| 11 | +# counts the number of iterations until the function diverges or |
| 12 | +# returns the iteration threshold that we check until |
| 13 | +def countIterationsUntilDivergent(c, threshold): |
| 14 | + z = complex(0, 0) |
| 15 | + for iteration in xrange(threshold): |
| 16 | + z = (z*z) + c |
| 17 | + |
| 18 | + if abs(z) > 4: |
| 19 | + break |
| 20 | + pass |
| 21 | + pass |
| 22 | + return iteration |
| 23 | + |
| 24 | +# takes the iteration limit before declaring function as convergent and |
| 25 | +# takes the density of the atlas |
| 26 | +# create atlas, plot mandelbrot set, display set |
| 27 | +def mandelbrot(threshold, density): |
| 28 | + # location and size of the atlas rectangle |
| 29 | + # realAxis = np.linspace(-2.25, 0.75, density) |
| 30 | + # imaginaryAxis = np.linspace(-1.5, 1.5, density) |
| 31 | + realAxis = np.linspace(-0.22, -0.219, 1000) |
| 32 | + imaginaryAxis = np.linspace(-0.70, -0.699, 1000) |
| 33 | + realAxisLen = len(realAxis) |
| 34 | + imaginaryAxisLen = len(imaginaryAxis) |
| 35 | + |
| 36 | + # 2-D array to represent mandelbrot atlas |
| 37 | + atlas = np.empty((realAxisLen, imaginaryAxisLen)) |
| 38 | + |
| 39 | + # color each point in the atlas depending on the iteration count |
| 40 | + for ix in xrange(realAxisLen): |
| 41 | + for iy in xrange(imaginaryAxisLen): |
| 42 | + cx = realAxis[ix] |
| 43 | + cy = imaginaryAxis[iy] |
| 44 | + c = complex(cx, cy) |
| 45 | + |
| 46 | + atlas[ix, iy] = countIterationsUntilDivergent(c, threshold) |
| 47 | + pass |
| 48 | + pass |
| 49 | + |
| 50 | + # plot and display mandelbrot set |
| 51 | + plt.imshow(atlas.T, interpolation="nearest") |
| 52 | + plt.show() |
| 53 | + |
| 54 | +# time to party!! |
| 55 | +mandelbrot(120, 1000) |
0 commit comments