A fractal tree is produced by one recursive rule applied to itself
at every scale: split into two shorter children that splay outward.
The same shape appears at every level of magnification — the hallmark of a fractal.
The Algorithm
branch(x, y, len, angle, depth):
if depth == 0:
draw leaf at (x, y) and RETURN ← base case
x2 = x + len × cos(angle)
y2 = y + len × sin(angle)
draw line (x,y) → (x2,y2)
branch(x2, y2, len×ratio, angle−spread, depth−1) ← left child
branch(x2, y2, len×ratio, angle+spread, depth−1) ← right child
Key Concepts to Notice
-
Base case and recursive case.
Every recursive function needs a stopping condition. Here it's
depth == 0. Without it the function would call itself forever.
-
Self-similarity.
Every sub-tree (grab any branch and cover the rest of the tree) is
a smaller, rotated copy of the whole tree. That is the definition of a fractal.
-
Exponential growth.
Each depth level doubles the number of branches: 2, 4, 8, 16 … 2N.
Watch the stats box and notice how quickly the segment count grows.
At depth 12 there are 4,096 leaf tips — the drawing noticeably slows.
-
Trigonometry.
The endpoint calculation
x2 = x + len × cos(angle) is
standard polar-to-Cartesian conversion. Change the angle and the branch
points in a different direction; change the length and it is shorter or longer.
-
The Spread slider controls how wide each fork opens.
Small angles (5°–15°) produce narrow, cypress-like spires.
Large angles (40°–55°) produce flat, spreading oak-like canopies.
-
The Length Ratio is the fractal scaling factor.
0.67 means each child is two-thirds the length of its parent —
a natural-looking proportion found in real trees.
-
Wind sway.
Tip branches sway more than trunk branches because they have more
leaf area above them and are farther from the trunk anchor.
The code models this by scaling sway by
(maxDepth − curDepth).
Connection to the Island Saga
The branch() function uses the exact same recursive pattern as
SWIsland._grow(): a base case that stops, then two (or more)
recursive calls with a smaller sub-problem. The difference is that the
island grows on a grid and can backtrack; the tree grows in continuous space
and never needs to.