Carl's Mandelbrot set viewer
Introduction
Oh boy, I like this one. Who would have thought that such a simple algorithm
would produce such a variety of fantastic images? And this is the first time
I have even come close to understanding complex numbers or seeing how they might
be useful. I got the idea from Roger Penrose's book The Road to Reality,
which is, like, whoa...
Click here to run the application (mandelbrot.exe v1.1
- 40KB). It requires version 2.0 of the .NET Framework, available here.
NOTE: The application is designed to run in the 'Internet zone', so when you click
the above link in Internet Explorer and then click Run you should be in business.
However, if you save the .exe and run it from your local hard drive then the zoom
selection works more smoothly, because it can use a fast technique that takes advantage
of the extra permissions given to a locally running application. Also, the
Save feature is disabled when the application is run from the Internet zone, again
for security reasons.
Click here to download all the C# source code (mandelbrot.zip
- 23 KB)
Please see below for quick links to the main C# source
files.
Using the Viewer
When the application starts it displays the entire Mandelbrot set, which looks like
a couple of circles with 'warts'. It might not look very interesting, but
hidden within it is an infinite space of intricate patterns. To see these,
you need to zoom in on a small section. To zoom, simply click and drag a rectangle
over the area of interest. Start by zooming in on one of the small 'warts'.
Then zoom in on one of the small areas within that. You should have no difficulty
seeing patterns such as these:
View Menu Commands
Refresh (F5) - Redraws the current view. Use this command
after you resize the Viewer's window.
Stop (Esc) - Cancels the rendering process. You will see
only the part of the image that has already been calculated, but you will still
be able to zoom anywhere in the Viewer's window. (Available only when drawing
is in progress).
Image Quality... - Displays a dialog box where you set the maximum
number of Mandelbrot iterations performed for each pixel. If |z| < 2 after
this number of iterations then the pixel will be coloured black. If |z| reaches
2 before the maximum number of iterations is reached then the image will be coloured
grey. An 80% grey pixel means that |z| exceeded 2 when 80% of the maximum
number of iterations had been performed. A higher maximum number of iterations
will produce a higher quality image (of a lighter colour), but will take longer
to render. The default is 1000, but you will need to increase it for very
high levels of magnification. The maximum is 99999.
Lock Aspect Ratio - If selected, automatically ensures that each
horizontal pixel represents the same sized physical space as each vertical pixel
(so if 100 pixels along the a axis cover a physical distance of 0.01 then 100 pixels
along the b axis will also cover a physical distance of 0.01). This is achieved
by widening or lengthening your zoom selection to give it the same width : height
ratio as the Viewer's window. If this option is not selected, the display
will be stretched or squeezed when your selected viewing area does not match the
shape of the Viewer's window.
Back (Backspace or Alt + Left Arrow) - Redraws the previous view
in the history (available only after you have zoomed in at least once).
Forward (Alt + Right Arrow) - Redraws the next view in the history
(available only after you have selected Back at least once).
Reset - Redraws the default view and clears the view history.
The Mandelbrot Set
What follows is my quick and dirty explanation of the Mandelbrot set. Please
bear in mind that I'm a software guy, not a mathematician, and I learned just enough
to write the program.
Every complex number c has a 'real' part a and an 'imaginary' part b. We write
c = a + bi, where i is the square root of -1. We can plot c on a 2 dimensional
plane, with a along the horizontal axis and b along the vertical axis. Then
the distance |c| from the origin (0,0) to (a,b) is sqrt(a² + b²)
To draw the Mandelbrot set, we need to perform a calculation on each point in the
plane that will determine its colour. What is this calculation? It is
an iterative process, in which the answer is fed back into the calculation
repeatedly. For a complex number c, we calculate the complex number zn as follows:
Obviously, to perform this iteration we need to calculate the square of a complex
number:
- c² = (a + bi)²
= a² + b²i² + 2abi
= a² - b² + 2abi (since i² = -1)
Notice that this gives us a new complex number with real part a² - b² and imaginary
part 2ab.
The Mandelbrot set consists of all those points for which |zn|
stays forever within a fixed boundary; these points are coloured black. If
|zn| eventually becomes infinite then c
is not in the Mandelbrot set and is coloured white. However, we can make the
display even more interesting by using shades of grey to indicate the speed with
which the calculation becomes infinite. The more iterations required before
reaching infinity, the darker the grey used to colour the point. It is known
that if |zn| > 2 then further iterations
will eventually lead to infinity, so we can stop as soon as |zn|
> 2. Of course, for performance reasons we don't want to calculate unnecessary
square roots, so we simply look for:
|zn| ² > 4; i.e. (an²
+ bn²) > 4.
Since we need an² and bn²
anyway, it's pretty efficient.
You will find the algorithm in the function MandelbrotSet.IsInSet().
This is called from CreateMandelbrotImage() in the MainForm form
As you will see, not much code is needed to create the image; most of the code in
the app deals with the zoom selection and view history.
Source Code
Here are the main C# files: