This is your basic Mandelbrot Set plotter. Very little effort made to speed it up, except for plotting top & bottom pixels together as they’re mirrored across the x-axis. Later I’ll show you some much faster programs!
The heart of this program is the iteration of z2+c where c is a point in the complex plane and z is initially complex 0. c is a member of the Mandelbrot Set if z remains bounded forever. In practice only a finite number of iterations is made, after which we assume the point is in the set. Also, it’s known that if z’s magnitude exceeds 2, then it will eventually escape.
It was originally written for Pygame, which I then found wasn’t set up for Python 3 on my computer. Converting it to Pillow was an easier option than dealing with the lengthy instructions I Googled for upgrading my Pygame (I’ll get around to it later…). I’ve left in the Pygame instructions as comments in case you want to go that way 🙂
# Python+Pillow/Pygame program to illustrate computing the Mandelbrot Set. # Note that it's far from efficient; it can easily be sped up! # Authour: Alan Richmond, Python3.codes #import pygame # see pygame.org from PIL import Image width, height = 1000,1000 # display window size #screen = pygame.display.set_mode((width, height)) # initialise pygame window img=Image.new("RGB",(width,height)) xaxis = width / 1.5 + 140 # scaling for x & y axes yaxis = height / 2 scale = 400 maxit = 99 # maximum iterations for iy in range(int(height/2)+1): # scan y-axis for ix in range(width): # scan x-axis z = 0 + 0j # initialise z=0 # map pixel position to complex plane c = complex(float(ix - xaxis) / scale, float(iy - yaxis) / scale) for it in range(maxit): # up to maximum iterations: z = z*z + c # iterate z^2 + c if abs(z) > 2: # z is flying off to infinity! col=(it % 4 * 64, it % 8 * 32, it % 16 * 16) # pick a colour break # break out of closest loop else: # loop finished so col = (0, 0, 0) # point is in set = colour black # screen.set_at((ix, iy), col) # set colour on top half # screen.set_at((ix, height-iy), col) # set colour on bottom half img.putpixel((ix,iy),col) img.putpixel((ix,height-iy-1),col) # pygame.display.update() # update window on screen #raw_input("Done") # picture disappears when Enter img.save("Mandelbrot.png", "PNG")
The Mandelbrot set is the set of complex numbers for which the function does not diverge when iterated from , i.e., for which the sequence , , etc., remains bounded in absolute value.
Its definition and name are due to Adrien Douady, in tribute to the mathematician Benoit Mandelbrot. The set is connected to a Julia set, and related Julia sets produce similarly complex fractal shapes.
Mandelbrot set images may be created by sampling the complex numbers and determining, for each sample point , whether the result of iterating the above function goes to infinity. Treating the real and imaginary parts of as image coordinates on the complex plane, pixels may then be colored according to how rapidly the sequence diverges, with the color 0 (black) usually used for points where the sequence does not diverge. If is held constant and the initial value of is variable instead, one obtains the corresponding Julia set for each point in the parameter space of the simple function.
Images of the Mandelbrot set exhibit an elaborate and infinitely complicated boundary that reveals progressively ever-finer recursive detail at increasing magnifications. The "style" of this repeating detail depends on the region of the set being examined. The set's boundary also incorporates smaller versions of the main shape, so the fractal property of self-similarity applies to the entire set, and not just to its parts.
The Mandelbrot set has become popular outside mathematics both for its aesthetic appeal and as an example of a complex structure arising from the application of simple rules. It is one of the best-known examples of mathematical visualization.