The Mandelbrot set is a fractal in the complex plane.
Many programmers write their own Mandelbrot generators as a way of sharpening their coding (and, to a lesser extent, maths) skills; there are interesting programming challenges to be solved, and a beautiful visual journey as you explore the depths of the set.
The algorithm
At its heart, plotting the Mandelbrot set is a question of complex maths:
- Map a grid of pixels to a region of the complex plane
- For each complex point, apply the formula
Zn+1 = Zn + C, where:
- C is the point's complex co-ordinates
- Z0 = 0
- Iterate each point until the point is distance greater than 2 from the origin, or until you get bored (often a fixed iteration limit).
- If a pixel did not escape, it is part of the Mandelbrot set and traditionally represented by a black pixel; otherwise, apply your choice of colouring algorithm to decide what colour to paint the pixel.
Challenges
In the process of writing a non-naive Mandelbrot set plotter there are a number of algorithmic and architectural challenges to be solved.
On deep zooms it is possible to reach the limit of precision of the double
type, leading to the question of how one might go further; arbitrary-precision arithmetic is one answer, while some brave souls implement their own fixed-point types.
Creating a plot is a very CPU-intensive process, so optimisation rapidly becomes important in order to be able to generate a plot in a reasonable time. As individual points do not depend on each other (with most plotting algorithms), it is possible to parallelise the work - which is itself a further challenge.
There is also the question of whether and how the user interacts with the plotter.