Algorithms

The Mandelbrot set: patterns at the boundary

Zoom into the gap between the large dark shape and the circle on its left. Swirls appear in the gap, and more patterns emerge inside them. With the Barnsley fern, we drew the picture from one long sequence of points. Here, every point in the picture gets its own sequence, and every sequence starts at zero.

Zoom into the boundary

Click or tap to zoom in on a point. Drag to move around. With the keyboard, use the arrow keys, + and −.

Calculating the view…

Zoom 1×Centre (-0.55; 0)

Early escapeLate escapeNo escape within the limit

Video walkthrough

One rule for the entire plane

Choose a point c on the complex plane. Its horizontal coordinate is the real part, and its vertical coordinate is the imaginary part. Start with z = 0. At each step, square the current z and add the chosen c. Use the result as the new z and repeat. Keep c fixed while checking that point.

The successive values of z form a sequence called an orbit. If the orbit stays within some finite distance of zero, c belongs to the Mandelbrot set. Each point in the picture corresponds to one choice of c.

When to stop calculating

Once z leaves the circle of radius 2 centred at zero, the orbit will grow without bound. For each point, we can count the steps until the first escape. At a distance of exactly 2, keep going, as in the example with c = 1.

For c = −1, the two values repeat forever, so this point belongs to the set. A point close to the boundary can take many more steps to escape.

Two choices of c on the horizontal axis
cOrbit from zeroResult
10 → 1 → 2 → 5Escapes at step 3
−10 → −1 → 0 → −1 → …Repeats

What the colours show

The explorer checks each point up to the selected step limit. Shades of blue mark early escapes, and turquoise and gold mark later ones. Nearly black marks points whose orbits have yet to escape when the calculation stops.

The dark region on screen is an approximation of the set. Some dark pixels may correspond to points that would escape with a higher limit. Raising it gives those pixels a chance to change colour. For points that already escaped, the step count and its colour stay the same.

What to look for as you zoom

Choose Seahorse Valley. This region is between the large shape and the round lobe on its left. Click or tap a swirl to make it the centre of your next view. Spiral close-up opens a deeper view of the same region.

Compare the spiral at limits of 160 and 640. The zoom and centre stay fixed, so the colour changes show where points escaped during the extra steps. The explorer calculates points for a new region of the plane each time you zoom. The finest detail you see depends on both the step limit and the image resolution.

Checking a pixel in JavaScript

Store z as a pair (zr, zi) and the chosen c as a pair (cr, ci). After squaring z, the real part is zr² − zi² and the imaginary part is 2 × zr × zi. Add the corresponding coordinate of c to each part. Both formulas use the previous values of zr and zi.

Compare the squared distance zr² + zi² with 4. The function returns the first step outside the circle, or 0 if the orbit stays within it for the allotted steps. The explorer runs this check on the coordinates at the centre of every pixel.

Show executable code
function escapeIterations(cr, ci, limit = 160) {
  let zr = 0;
  let zi = 0;

  for (let step = 1; step <= limit; step++) {
    const nextReal = zr * zr - zi * zi + cr;
    zi = 2 * zr * zi + ci;
    zr = nextReal;

    if (zr * zr + zi * zi > 4) return step;
  }
  return 0;
}

console.log(escapeIterations(1, 0));  // 3
console.log(escapeIterations(-1, 0)); // 0

Time and memory

With P pixels and a limit of N steps, drawing takes up to O(P × N) operations. Points that escape early take less work. Checking a single point requires O(1) extra space. The explorer holds O(P) bytes for the image and O(N) for the colour table, and runs the calculation in a separate browser thread.

Source

Robert L. Devaney, The Mandelbrot Set (Boston University)