14. The Mandelbrot Set
Infinite complexity from one simple equation — rendered pixel by pixel
Take a complex number. Square it. Add the original number. Square the result. Add the original again. Keep going. If the value stays bounded forever, the number is in the Mandelbrot set. If it flies off to infinity, it's out. That's the entire algorithm — and it produces one of the most famous images in mathematics.
Complex Numbers in 30 Seconds
A complex number has two parts: a real part and an imaginary part. We write it as:
where a is the real component, b is the imaginary component, and i is the imaginary unit (i2 = -1).
For our purposes, think of a complex number as a 2D point: the real part is the x-coordinate, the imaginary part is the y-coordinate. The complex plane is just a regular Cartesian plane with different labels.
Complex multiplication
To square a complex number z = x + yi:
z2 = (x2 - y2) + (2xy)i
The new real part is x2 - y2. The new imaginary part is 2xy. No complex-number library needed — just basic arithmetic on two floats.
The Iteration
The Mandelbrot set is defined by iterating this function:
We start with z0 = 0 and a chosen constant c (the point we're testing). Then we iterate:
| Step | Formula | Result |
|---|---|---|
| 0 | z0 = 0 | 0 |
| 1 | z1 = 02 + c | c |
| 2 | z2 = c2 + c | c2 + c |
| 3 | z3 = (c2 + c)2 + c | ... |
If the magnitude of z (its distance from the origin) ever exceeds 2, it's mathematically guaranteed to escape to infinity. We call that our bailout condition.
If z hasn't escaped after some maximum number of iterations, we assume the point is in the set.
Escape Time Algorithm
Here's the core algorithm in pseudocode:
for each pixel (px, py) on screen:
c = map (px, py) to complex plane
z = 0 + 0i
iteration = 0
while |z| <= 2 and iteration < MAX_ITERATIONS:
z = z * z + c
iteration++
color the pixel based on iteration count
The iteration count at which each point escapes determines its color. Points that never escape (iteration hits the maximum) are colored black — they're in the set. Points that escape quickly get one color; points that escape slowly get another. This gradient creates the intricate boundary patterns the Mandelbrot set is famous for.
Mapping pixels to the complex plane
The classic Mandelbrot view spans roughly:
- Real axis: -2.5 to 1.0
- Imaginary axis: -1.25 to 1.25
To map a pixel coordinate (px, py) on a canvas of size (width, height) to this region:
real = minReal + (px / width) * (maxReal - minReal)
imag = minImag + (py / height) * (maxImag - minImag)
Coloring
The simplest approach maps the escape iteration to a color. Points inside the set (max iterations reached) are black. For escaped points, there are several strategies:
Linear grayscale
Map the iteration count directly to a brightness value:
brightness = iteration / MAX_ITERATIONS * 255
This works but produces visible banding — discrete jumps between colors.
Smooth coloring
To eliminate banding, use smooth iteration counts. When a point escapes at iteration n with magnitude |z|:
This gives a continuous floating-point value instead of a discrete integer, producing silky gradients.
HSL cycling
Map the smooth iteration count to a hue value and cycle through the color spectrum:
hue = (smooth * 10) % 360
color = hsl(hue, 80%, 50%)
This is the technique used in the explorer below.
Try It: Mandelbrot Explorer
Click and drag to select a region to zoom into. Use the controls to adjust iteration depth and coloring.
Mandelbrot Explorer
Why the Boundary Is Infinitely Complex
The Mandelbrot set's boundary is a fractal — it has infinite detail at every scale. No matter how far you zoom in, you find the same kinds of spirals, tendrils, and miniature copies of the full set (called mini-brots). This self-similarity isn't exact repetition; it's a family resemblance that shifts and varies at every level.
This happens because the boundary is where the iteration's behavior transitions between escaping and staying bounded. At the boundary, even the tiniest change in c can flip a point from inside to outside (or vice versa). That sensitivity creates structure at every magnification.
The boundary's fractal dimension is exactly 2 — it's so convoluted that it fills as much area as a 2D surface, despite being a 1D curve in the topological sense.
Relationship to Julia Sets
Every point c in the complex plane has a corresponding Julia set — the set of starting values z0 for which the iteration zn+1 = zn2 + c stays bounded.
- If c is inside the Mandelbrot set, its Julia set is a connected, blob-like shape.
- If c is outside the Mandelbrot set, its Julia set shatters into disconnected dust (a Cantor set).
The Mandelbrot set is, in a sense, a catalog of all Julia sets — it tells you which values of c produce connected Julia sets and which produce dust.
Performance: Web Workers
Rendering the Mandelbrot set means running the escape-time loop for every single pixel. On an 800x600 canvas, that's 480,000 points, each potentially iterating hundreds of times. This is embarrassingly parallel — each pixel is independent — making it a perfect fit for Web Workers.
The explorer above splits the canvas into horizontal strips and farms each strip out to a worker. Each worker computes escape times for its strip and posts the pixel data back. The main thread assembles the strips into a single image. On a modern machine with multiple cores this produces a noticeable speedup over single-threaded rendering.
What's Next
The Mandelbrot set is just the beginning of generative art with math. Future articles in this section will explore noise functions, L-systems, strange attractors, and more techniques for creating visual complexity from simple rules.