The idea: four maps and a random choice

Michael Barnsley found that a fern can be drawn with four simple rules. Start with the point (0, 0). Pick one of four affine maps at random, apply it to the point, plot the new point, and repeat. After a few thousand steps the dots form a fern. This is the chaos game, and the four maps together are an iterated function system (IFS).

The Barnsley fern, 60,000 points coloured by the map that made each point: a short brown stem from f₁, the green fern from the second pair of leaflets up from f₂, the blue lowest left leaflet from f₃ and the purple lowest right leaflet from f₄
Coloured by the map that made each point, the fern splits into four parts: each part is a smaller copy of the whole fern.

The four maps

Each map takes a point (x, y) to (a·x + b·y + e, c·x + d·y + f): a 2×2 matrix that stretches, turns or flips, then a shift (e, f). That is an affine map, as on Affine Transforms. These are Barnsley's numbers:

mapabcdefpdraws
f₁0000.16000.01the stem
f₂0.850.04−0.040.8501.60.85the rest of the fern: smaller and smaller leaflets
f₃0.2−0.260.230.2201.60.07the largest left leaflet
f₄−0.150.280.260.2400.440.07the largest right leaflet

To pick a map, the page draws a random number r between 0 and 1 and looks where it falls on a bar split 0.01 / 0.85 / 0.07 / 0.07: r < 0.01 means f₁, r < 0.86 means f₂, and so on. The numbers come from a seeded random generator (seed 1), so every run, every undo and every replay gives exactly the same points. (Demo: the first 10 points one by one.)

  • f₁ sets x to 0 and keeps 0.16 of y: it squashes everything onto a short piece of the y-axis.
  • f₂ shrinks by about 0.85, turns by −2.7° (clockwise) and lifts by 1.6.
  • f₃ shrinks to about a third, tips the fern about 50° to the left and lifts it by 1.6.
  • f₄ shrinks to about a third, tips it about 49° to the right, mirrors it (its determinant is negative) and lifts it by 0.44.

Why the dots form a fern: the attractor

Apply all four maps to the whole fern F, not just to one point. f₁ squashes it into the stem, f₂ turns it into the fern without its stem and lowest pair of leaflets, f₃ and f₄ turn it into the two lowest leaflets. Put together, the four copies are the fern again:

F = f₁(F) ∪ f₂(F) ∪ f₃(F) ∪ f₄(F)

Every map here is a contraction: it brings any two points closer together. For such a system there is exactly one closed, bounded set that equals the union of its images (Hutchinson's theorem); it is called the attractor. Start the game anywhere and the point is pulled toward it; start on it, as (0, 0) is (the fixed point of f₁), and it never leaves. That is why the colour of a dot tells you which copy it sits in: a dot made by f₃ is f₃ of a fern point, so it lies in f₃(F). (Demo: what each map does: the fern is 4 copies of itself.)

Left: the whole fern F in grey. Right: the grey fern with f₂(F) drawn over it in green: the copy covers everything except the stem and the lowest left and right leaflets, which are left for f₁, f₃ and f₄
f₂ maps the whole fern onto the fern minus its stem and lowest pair of leaflets; f₁, f₃, f₄ fill exactly what is left.

Each leaflet is itself a small fern with its own leaflets, and so on: the fern is self-similar. That only holds down to the size of a dot, but the attractor itself has detail at every scale.

The probabilities change the density, not the shape

The attractor is fixed by the four maps alone; p does not appear in the equation above. What p changes is where the points spend their time. With p = 0.25 for each map, a quarter of all points land on the stem, a thin line, and the top, which needs many f₂ steps in a row, gets almost none: to climb above y ≈ 8.2 takes about 11 f₂ picks in a row, chance 0.25¹¹ ≈ 1 in 4 million, so 100,000 points never get there. Every point that does appear lies on the same fern; the page draws Barnsley's fern in light grey behind them to show it. (Demo: equal probabilities: same fern, drawn unevenly.)

Two ferns with 20,000 points each. Left, Barnsley's probabilities 0.01, 0.85, 0.07, 0.07: evenly filled. Right, equal probabilities 0.25 each: green points crowd the stem and the lower leaflets, all on top of the light grey Barnsley fern, and the upper part of the fern gets no points
Same maps, same number of points: the probabilities only decide where the points pile up, never where they can land.

Barnsley chose the probabilities roughly in proportion to the area of each copy (|det| of the matrix), with a small share for the stem, so that the fern fills in evenly.

One number reshapes the plant

f₂'s matrix is 0.851 times a rotation by θ = −2.69°. The page keeps 0.851 and the other three maps and only changes θ. Because every leaflet higher up was made by f₂ applied again and again, the small turns add up along the frond. (Demo: turn f₂ by −2.7°, 0°, −5°, +2.7°.)

f₂ turn θwhat the fern doesheight
−2.7° (Barnsley)curls gently to the right10.0
0°grows straight up10.7
−5°bends over into a hook8.9
+2.7°curls to the left10.0

Change all four maps and you get other species. The Cyclosorus fern uses f₂ = (0.95, 0.005, −0.005, 0.93) with a shift of (−0.002, 0.5), and narrower leaflets from f₃ and f₄. (Demo: the Cyclosorus fern, 4 other maps.)

The maps are built from the moves on Rotation Matrices, Linear Transformations and Composing Transforms: every dot is a long composition of them. The same game with three maps that halve the distance to a corner draws the Sierpiński triangle on The Chaos Game; curves built by repeated replacement are on Fractal Curves. Another simple rule repeated until chaos appears: The Logistic Map.

What the page leaves out

The proof of Hutchinson's theorem (the maps shrink distances between sets in the Hausdorff metric, so the Banach fixed-point theorem gives one attractor), the fractal dimension of the fern, the deterministic way to draw an attractor (apply all maps to a whole picture again and again), how Barnsley's collage theorem finds maps for a given picture, other seeds, and 3D ferns. The window is fixed at x ∈ [−3.3, 4.3], y ∈ [−0.6, 11], and the page stops at 100,000 points.