# Python PyGame: 2.1 (Mandelbrot)

Python PyGame:2.1 Mandelbrot (a short aside)

In my last blog I started off with a game that draws circles of different colors. That is boring. Personally, I never tire of rewriting programs to draw fractals. Today we’ll quickly jump through a program to draw the Mandelbrot set. Drawing 2d fractals may be so 1980, but, I get a kick out of it.

What is a fractal? Simply put, a fractal is worlds within worlds. It’s a geometric shape that no matter how far you zoom into or out of still holds the original shape. It’s a fairly new term coined by Benoit Mandelbrot in 1982 in his book, “The Fractal Geometry of Nature”.

More importantly, fractals are the closest approximation we have come up with for a Mathematics of Nature. They can approximate clouds, coastlines, snowflakes and earthquakes. It’s related to the popularization of Chaos Math by Edward Lorenz and his study of weather prediction in 1961. I cannot claim understanding, but, I do find it fascinating to ponder over. More on that after the code if you’re interested.

Python & PyGame by themselves don’t just carry around the code necessary to pull this off. This gives us an excuse to play with two cool libraries: NumPy and PIL (Python Imaging Library).

http://numpy.scipy.org/

http://www.pythonware.com/products/pil/

Once again, if you’re running Windows 32 bit, life is obvious, if you’re running 64 bit, you must download them from here:

http://www.lfd.uci.edu/~gohlke/pythonlibs/

Next I happened across a version of the program written in 2006. Updated it a bit to work with the newer libraries, and the result:

Run it

You’ll see:

That’s much cooler than a bunch of circles.

Let’s quickly review what some of those NumPy functions do.

Arange – numpy.arange([start], stop[, step], dtype=None, maskna=False)

returns an evenly spaced set of values. Sp, arange(3)=[0,1,2]

Ravel – numpy.ravel(a, order=’C’)

Returns a flattened array. So a two dimensional array of [123] and [abc] is returned as [123abc]

Shape – ndarray.shape

Tuple of array dimensions.

Zeros – numpy.zeros(shape, dtype=float, order=’C’)

Return a new array of given shape and type, filled with zeros.

Here’s some further amateur discussion:

Free Will or Determinism is one of philosophy’s Coke vs. Pepsi discussions. You’ve seen it again and again through popular media: books (Ender’s Game) or movies (Matrix, Inception). In mathematics it has shown up as well. Newtonian Mechanics is pro-determinism. Then Quantum Mechanics became popular, which many felt was more aligned with Free Will. Then Chaos Math came back as a pro-determinism mathematics that can solve a lot of seemingly “random” problems in math: cloud formations, turbulence, weather patterns. The fractal is a mainstay in Chaos Math. Totally predictable and matches up with a lot of patterns in nature. Unfortunately there remain many unsolved problems in these mathematics, but it is fun for me to think about when I’m having trouble with a simple coding problem.

Here’s some more links:

This web page explains the math, theory, and how to do this in c++:

http://warp.povusers.org/Mandelbrot/