# Naive Mandelbrot Set

By Monty, 17th April 2015

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")
Wikipedia:

The Mandelbrot set is the set of complex numbers ${\displaystyle c}$ for which the function ${\displaystyle f_{c}(z)=z^{2}+c}$ does not diverge when iterated from ${\displaystyle z=0}$, i.e., for which the sequence ${\displaystyle f_{c}(0)}$, ${\displaystyle f_{c}(f_{c}(0))}$, 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 testing, for each sample point ${\displaystyle c}$, whether the sequence ${\displaystyle f_{c}(0),f_{c}(f_{c}(0)),\dotsc }$ goes to infinity (in practice -- whether it leaves some predetermined bounded neighbourhood of 0 after a predetermined number of iterations). Treating the real and imaginary parts of ${\displaystyle c}$ as image coordinates on the complex plane, pixels may then be coloured according to how soon the sequence ${\displaystyle |f_{c}(0)|,|f_{c}(f_{c}(0))|,\dotsc }$ crosses an arbitrarily chosen threshold, with a special color (usually black) used for the values of ${\displaystyle c}$ for which the sequence has not crossed the threshold after the predetermined number of iterations (this is necessary to clearly distinguish the Mandelbrot set image from the image of its complement). If ${\displaystyle c}$ is held constant and the initial value of ${\displaystyle z}$—denoted by ${\displaystyle z_{0}}$—is variable instead, one obtains the corresponding Julia set for each point ${\displaystyle c}$ 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.