17. L-Systems

Growing fractal trees and plants from string-rewriting rules

In 1968, biologist Aristid Lindenmayer introduced a formal grammar for modeling plant growth. The idea is disarmingly simple: start with a string of characters, apply rewriting rules repeatedly, then interpret the final string as drawing instructions. A few iterations of a one-line rule can produce branching trees, ferns, snowflakes, and organic structures of startling complexity.

The Core Idea

An L-system has three components:

  1. Alphabet — a set of symbols (e.g., F, +, -, [, ])
  2. Axiom — the initial string (the "seed")
  3. Production rules — rules that replace symbols each generation

Each generation, every symbol in the string is replaced simultaneously according to the rules. Symbols with no rule are left unchanged. This parallel rewriting is what distinguishes L-systems from sequential grammars like Chomsky's.

A first example: algae growth

Lindenmayer's original system modeled algae:

Alphabet A, B
Axiom A
Rules A → AB, B → A
Generation String
0 A
1 AB
2 ABA
3 ABAAB
4 ABAABABA

The string lengths follow the Fibonacci sequence: 1, 2, 3, 5, 8, 13... This is already interesting mathematically, but L-systems become visually powerful when we interpret the strings as drawing commands.

Turtle Graphics

The standard way to render an L-system is turtle graphics. Imagine a turtle sitting on the canvas with a position, a heading angle, and a pen. It reads the string character by character:

Symbol Action
F Move forward by step length, drawing a line
f Move forward without drawing
+ Turn right by angle δ
- Turn left by angle δ
[ Push current state (position + angle) onto a stack
] Pop state from the stack (jump back to saved position)

The branching comes from the stack. When the turtle encounters [, it saves where it is. It draws a branch, then ] teleports it back to the fork point to draw the next branch.

Classic L-System Plants

Koch Snowflake

Axiom F--F--F
Rules F → F+F--F+F
Angle 60°

Each straight segment is replaced by a segment with a triangular bump. After a few iterations, the boundary becomes a fractal curve with infinite length enclosing finite area.

Sierpinski Triangle

Axiom F-G-G
Rules F → F-G+F+G-F, G → GG
Angle 120°

Two symbols (F and G) both draw forward, but have different replacement rules. The result is the classic Sierpinski triangle gasket.

Fractal Plant

Axiom X
Rules X → F+[[X]-X]-F[-FX]+X, F → FF
Angle 25°

The symbol X is a variable — it's rewritten by the rules but produces no drawing action. It serves purely as structural scaffolding. This system generates remarkably realistic branching plants.

Dragon Curve

Axiom FX
Rules X → X+YF+, Y → -FX-Y
Angle 90°

X and Y are non-drawing variables. The result is the Heighway dragon — a space-filling curve that tiles the plane.

Try It: L-System Explorer

Choose a preset or define your own axiom, rules, and angle. Adjust the number of generations and watch the structure grow.

L-System Explorer

String length: —

How the String Grows

L-system strings grow exponentially. If every F is replaced by a string containing two Fs, the string doubles each generation. After n generations, the string length is roughly O(2n). This is why even modest generation counts (6-8) produce thousands of drawing commands and rich visual detail.

Generations Koch segments Plant string length
1 4 ~10
2 16 ~30
3 64 ~100
4 256 ~350
5 1,024 ~1,200
6 4,096 ~4,000

This exponential growth is also why there's a practical limit to how many generations you can render — the string and the number of line segments can quickly exceed millions.

Stochastic L-Systems

Deterministic L-systems always produce the same shape. Stochastic L-systems add variety by assigning probabilities to rules. If a symbol has multiple possible replacements, one is chosen at random each time:

F → F[+F]F[-F]F    (probability 0.33)
F → F[+F]F          (probability 0.33)
F → FF[-F]           (probability 0.34)

Each run produces a different tree with the same general character — the same branching strategy but different specific branching choices. This is how L-systems can model the variation you see in real forests: every tree of the same species looks similar but no two are identical.

Context-Sensitive L-Systems

The L-systems we've seen so far are context-free — each symbol is rewritten regardless of its neighbors. Context-sensitive L-systems let rules depend on what's next to the symbol:

A < B > C → D

This rule says: replace B with D, but only if B is preceded by A and followed by C. Context sensitivity lets you model signals propagating through a plant — for example, a flowering signal traveling from root to tip over multiple generations.

Parametric L-Systems

Parametric L-systems attach numeric parameters to symbols, enabling continuous variation:

A(x) : x > 1 → A(x-1) B(x*0.5)
A(x) : x <= 1 → C

Parameters can control branch length, thickness, curvature, or any other property. This lets a single system produce smoothly varying structures — branches that taper, leaves that grow, or segments that curve gradually.

L-Systems and Fractals

Many classic fractals are L-systems in disguise:

  • Koch curve — F → F+F--F+F at 60°
  • Sierpinski triangle — built from two drawing symbols
  • Dragon curve — two non-drawing variables weaving forward segments
  • Hilbert curve — a space-filling curve from 90° turns
  • Peano curve — another space-filler at 90°

What makes L-systems powerful is that they express these fractals generatively — as a growth process rather than a static definition. You don't describe the final shape; you describe how it grows, and the shape emerges.

What's Next

L-systems grow structure from deterministic rules. Next we'll explore strange attractors — systems where simple equations iterated millions of times trace out beautiful chaotic trajectories in phase space.