In this article I’ll show how to render the Mandelbrot set using Haskell, displayed in a window using wxWidgets / wxHaskell.
Note: The code I’ve written here makes no attempt to be fast. In fact, the code I present here is a lot slower than the version I originally wrote, because I’ve decided to go for code simplicity. I’ll note any particularly slow bits of the code as I go along, but please don’t think the lack of performance is due to the language used.
I’m going to present this code backwards – starting from the bottom-most function, and working my way towards main.
The Mandelbrot Set is (in simplified terms) a way of categorizing points in the two-dimensional complex plane: some points are in the set, and some points are not. In the image above, the black pixels are in the Mandelbrot set.
The algorithm I’ve chosen to render the Mandelbrot set is the escape algorithm, which is probabilistic – it determines that points are likely to be in the set (or not), but it cannot say for certain. We must pick a trade-off between performance and accuracy.
Given a point c on the complex plain, we keep iterating until its distance from the origin
is greater than two, or until we reach a set limit. If we reach the limit of
we mark the point as being in the set, and return
Nothing. If we escape, we return
N is a scaled value indicating the number of iterations required to escape.
Strictly speaking, that number N is irrelevant to the set, but it allows us to draw pretty
colors around the set. On each iteration, we set z’ to be z2 + c.
Performance Note: Taking the magnitude of the current complex value is a very slow way of working out whether we’ve escaped or not – a much better way would be to escape if the absolute value of either the real or imaginary parts of the value exceed two.
Now that we can compute a value for any point on the complex plain, we’ll need to convert it to an RGB value.
Points in the set (
Nothing) are colored black, while points not in the set are given a color
ranging from white to blue, by squaring or square-rooting the scaled iteration count for each of
the red, green, and blue components.
Performance Note: Again, using a square-root function here is very slow. It would be far better to use some form of lookup-table to assign colors.
When I wrote the original version of this program, the output appeared very pixellated. I decided to add 4x anti-aliasing to smooth things out a bit. Essentially, for each point in the image, I pick four sub-pixel points, get the color for each, then average them.
colorMandelbrot function takes a list of offsets for anti-aliasing, as well as an x-y point in
the complex plane. That gets converted to a complex number using the
Picking anti-alias offsets makes use of the scaling function. It picks arbitrary neighbour pixels, and scales them to find the actual distance between them. I’ve hard-coded the zoom and translation parameters here to show the whole set, but you could tweak them to display different parts off the set.
<*> are just being used here to produce combinations of coordinates
– a list comprehension or ‘do’ block would have worked just as well.
As we move up the code, we move away from the mathematical description of the set, towards the messy realities of actually drawing it in a window. I’m going to render the set as a bitmap, and to create that bitmap, I need to create a stream of color data. I’ll create the color data from a stream of coordinate data.
For a given bitmap width and height, this function gives a list of the actual complex plane
coordinates to color. The
y values might seem like they’re the wrong way around, but
that’s necessary to get the data stream order correct.
Finally, I can actually create the bitmap:
So I now have a bitmap ready to be drawn. For simplicity, I’m just going to draw it to a fixed-size window.
This results in a windows that looks a little like the following:
So there we go. Fancy improving my code? Here’s a few ideas:
- See just how fast you can make this code.
- Allow the user to resize the main window.
- Allow the user to zoom in/out and pan around – possibly add some controls to allow this.
- Fix the application not responding while rendering the set.
- Experiment with variations on the Mandelbrot set – try different powers or variations on the iteration z’ <- z2 + c.