Benoit Mandelbrot died a few days ago, at the age of 85. He is best known for discovering and popularizing the concept of fractals, in particular the Mandelbrot set and its close relative the Julia set.
In doing so, Mandelbrot gave the world much more than just a way to create pretty pictures and psychedelic animations. The realisation that such a literally infinite amount of complexity, not to mention beauty, could be created with a formula which you can write on the back of your hand, influenced thinkers in just about every field from philosophy to economics and from physics to biology.
As a little tribute to the man, if you are a programmer, I hereby encourage you to implement his famous fractal in your favourite programming language. It’s really simple; in many programming environments, opening a canvas to draw the pixels onto will be more work than actually calculating the fractal.
Here is how it goes: first, you need to pick a portion of the complex plane to visualize. To get the famous, fully zoomed-out version of the Mandelbrot, a good starting point would be from -2.5 + 1.5i to 1.5 – 1.5i.
Not comfortable with complex numbers? No problem; for our current purpose, all you need to know is that a complex number is a point in a two-dimensional space; in this case, the space from (x = -2.5, y = 1.5) to (x = 1.5, y = -1.5). And that there are certain rules for doing basic arithmetic on them, such as:
- Addition: (x1, y1) + (x2, y2) = (x1 + x2, y1 + y2)
- Multiplication: (x1, y1) (x2, y2) = (x1 x2 – y1 y2, x2 y1 + x1 y2)
Now, on to the Mandelbrot fractal. Formally, the Mandelbrot set is a set of points; any point is either inside the set or outside it. To determine whether a certain point p is inside the set, you calculate:
z0 = 0
zn+1 = zn2 + p
repeatedly. If this sequence goes to infinity for large n, the number is outside of the set; otherwise it is inside the set. Draw all the points which are inside the set in one color, and all the points which are outside the set in another, and you get the famous Mandelbrot fractal!
From a programmer’s point of view, there are two obvious questions here: a) can you please be a bit more specific about “goes to infinity for large n”, and b) most of the Mandelbrot pictures I’ve seen have much more than two colours!
The answer to both questions is: what you normally do is, you continue iterating until the distance between z and (0, 0) exceeds 2, and you colour the pixel based on how many iterations it took to get there. If, after a certain arbitrary number of iterations, you still haven’t reached 2, then you assume that the pixel is inside the set; the traditional colour for those pixels is black.