Mandelbrot Magic - Python

Bookmark and Share

Back to Games Mandelbrot Home Pictures 1 Pictures 2 Python Code

The Mathematics of Mandelbrot

The Mandelbrot set is a set of points in the complex plane that share an interesting property. Choose a complex number c . With this c in mind, start with z0 = 0. Then repeatedly iterate as follows: zn+1 = zn2 + c


The Mandelbrot set is the collection of all complex numbers c such that this process does not diverge to infinity as n gets large. There are other, equivalent definitions of the Mandelbrot set. The Mandelbrot set consists of those points in the complex plane for which the associated Julia set is connected.


The Mandelbrot set is a fractal, meaning that its boundary is so complex that it can not be well-approximated by one-dimensional line segments, regardless of how closely one zooms in on it. In fact, the Mandelbrot set's boundary has a dimension of 2, though the details of this are better left to the many available references. If you're interested, Wikipedia is always a good place to start; there is another good resource here

The Mandelbrot Set in Python!

For this section, you will need to install Python. You can download it here. While I am positive the code runs on Python 2 (i.e. 2.5, 2.6, etc), I have no idea if it works in Python 3. You will also need the bmp.py and mandelbrot.py files.


The core of the mandelbrot code is in inMSet(c,n) which returns True if complex number c is in the mandelbrot set after n iterations. The mset(width, coordinateList) function saves a bitmap of the Mandelbrot set in black and white. mSetColor does the same thing, but in color. drawPic is a helper function that speeds up the process by saving what pixels it drew last, since often neighboring pixels have the same color. Finally, changeColor cycles through the rainbow.


The primary function is main. As written, it will zoom onto the point (-0.743647477037151, -0.131830275205330i), generating 25 images that are 2000x2000 pixels in size. Something this large will take a while to run, but you can use it to generate arbitrarily detailed images.

The Mandelbrot Set in VPython!

The next part of this demonstration requires VPython and PIL (Python Imaging Library). VPython is an easy to use 3D graphics system for Python. If you would like a brief introduction to VPython, a good one is availiable here. First, convert all of your BMP images generated above to JPGs. There's probably a Python libary for this, but doing it manually isn't hard.


The main1() and main2() functions generate two different VPython mandelbrot patterns. First, a scene is constructed, on which everything is drawn. You can easily change the title of the window and its size. Turning off autoscaling usually helps when rotating the window. mySphere draws a sphere of a certain position, radius, opacity, and num (mandelbrot set number). If this is the first time you're running it, the getTex function generates a TGA texture file from the JPG. If you want to change your image, you have to delete the texture file for a new one to be made. The spheres are loaded with the texture onto the scene. You can change where they are loaded, how large they are, their transparency, and their radius to make your own mandelbrot set artwork.


Once you run your function, you can use your mouse to rotate around, and even inside, the scene. If your mouse has a scroll wheel, you can use this to zoom in or out of your scene. If you have any awesome ones you'd like to post, email admin@testfrenzy.com.