Barnsley Fern¶
By the end of this lab you'll be able to:
- Use probability-weighted random choice to apply one of four transformations per step
- Plot thousands of points with
dot()to build up an image gradually - Understand Iterated Function Systems (IFS) — how a few affine transforms create natural shapes
After plotting 5000 random points, a photorealistic fern leaf emerges from pure mathematics. No drawing loops, no branches — just four transformations and probability.
Welcome to the Barnsley Fern!
Michael Barnsley discovered in 1988 that you can generate realistic leaf shapes
from just four simple mathematical transformations applied randomly.
This lab shows you how! Let's code it together!
How It Works¶
Four transformations are applied randomly, each chosen with a fixed probability:
1. Stem (1% chance): (x, y) → (0, 0.16y)
2. Small leaflets (7%): (x, y) → (0.85x + 0.04y, -0.04x + 0.85y + 1.6)
3. Left frond (7%): (x, y) → (0.2x - 0.26y, 0.23x + 0.22y + 1.6)
4. Right frond (85%): (x, y) → (-0.15x + 0.28y, 0.26x + 0.24y + 0.44)
Starting at (0, 0), apply a random transformation, plot the point, repeat 5000 times.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
What Do You Think Will Happen?
5000 random points placed one at a time — will they look random and scattered,
or will a recognizable shape emerge?
Make your guess — then click Run to find out! (This will take a moment.)
Try It Now¶
A photorealistic fern leaf emerges from random points — no chaos, just probability revealing the attractor. Were you right?
How It Works¶
The four transformations together form an Iterated Function System (IFS). The 5000 points converge to the "strange attractor" of the system — the set of points that stays invariant under these transformations. Because the transformations encode the self-similar structure of a fern, the attractor looks like a fern.
Explanation Table¶
| Line | What it does |
|---|---|
random.seed(42) |
Same seed → same "random" sequence each run |
r = random.random() |
Choose a transformation by probability |
0.01 / 0.85 / 0.07 / 0.07 |
Probabilities of the four transformations |
scale = 35 |
Scale factor: IFS coords are -3 to 3 → canvas coords |
dot(2, 'forestgreen') |
Plot one tiny green dot |
Learning Check¶
Your Turn — Change the Leaf Color
Change 'forestgreen' to 'mediumseagreen' for the main color.
Then add a second pass with dot(1, 'limegreen') to add bright highlights.
Run it twice and compare the colors!
Coloring by y-position gives the fern a natural gradient — darker at the base, brighter at the tip — mimicking how real leaves look in sunlight.
Experiments¶
-
Double the points. Change
range(5000)torange(10000). The fern fills in more densely. You'll know it worked when there are fewer gaps in the leaf. -
Use a different seed. Change
random.seed(42)torandom.seed(7). The exact fern pattern changes but the overall shape stays the same. -
Change the dot size. Try
dot(3, 'forestgreen'). Larger dots fill in faster but look coarser. You'll know it worked when the fern is thicker. -
Print the first point. Add
print(f"First point: {x:.3f}, {y:.3f}")inside the loop after the first iteration. You'll know it worked when you see the first transformed coordinate.
Nature From Numbers!
You grew a fern from four lines of math and a random number generator!
Iterated Function Systems are used in image compression and procedural art.
Up next: Anti-Koch Snowflake — bumping inward instead of outward.