HomeArticlesGenerative Art

Turtle Graphics & L-Systems: From String Rewriting to Fractal Trees

A tiny string-rewriting rule, applied over and over, turns into a fern, a dragon curve, or a Koch snowflake once a turtle draws it.

mysimulator teamUpdated July 2026≈ 7 min read▶ Open the simulation

Rewriting a string into thousands of characters

A Lindenmayer system (L-system), introduced in 1968 by biologist Aristid Lindenmayer to model algae growth, starts from one symbol — the axiom — and repeatedly replaces every symbol in the string with a longer one according to fixed production rules. Lindenmayer's own example uses the axiom A with rules A→AB, B→A; after four generations, A has grown into ABAABABA, and the string length follows the Fibonacci sequence exactly. The rewriting is context-free — a symbol's replacement never depends on its neighbours — yet the aggregate result still captures the self-similar branching of real ferns and trees, because biological growth itself is a repeated local rule applied at every growing tip.

Turtle graphics: from string to pixels

A string of letters isn't a picture until a turtle — a cursor from Seymour Papert's 1967 Logo language, carrying a position and a heading angle — interprets it character by character. F/G moves forward while drawing a line; f moves without drawing; + and - turn left or right by a fixed angle δ; [ pushes the current position and heading onto a stack, and ] pops it back — the mechanism that lets a stem split into two branches and then resume exactly where the main stem left off. Only two numbers — step length and turn angle δ — control the whole visual family; changing δ from 20° to 25° can turn a tidy geometric pattern into a wild organic one.

Koch curve:       axiom F,  rule F → F+F--F+F,           δ = 60°
Dragon curve:     axiom FX, rules X→X+YF+, Y→-FX-Y,       δ = 90°
Sierpiński arrow: axiom A,  rules A→B-A-B, B→A+B+A,       δ = 60°
Branching plant:  axiom F,  rule F → F[+F]F[-F]F,         δ = 25°

Fractal cousins and stochastic variation

L-systems are a general-purpose fractal generator closely related to iterated function systems — the Koch snowflake and dragon curve have equally clean L-system definitions alongside more organic shapes like Barnsley's fern (axiom X, rules X→F+[[X]-X]-F[-FX]+X, F→FF, δ=25°). Real plants aren't perfectly self-similar, so a stochastic L-system gives a symbol two or more alternative replacement rules and picks between them randomly, weighted by probability, each time it fires — breaking the mechanical regularity and producing far more convincing, nature-like variation between branches.

Frequently asked questions

What is an L-system?

A Lindenmayer system (introduced in 1968 by biologist Aristid Lindenmayer to model algae growth) starts with one symbol, the axiom, and repeatedly rewrites every symbol in the string using a fixed set of production rules. After a handful of generations a single character grows into thousands of characters, which a turtle interpreter can then draw as a plant, snowflake, or curve.

How does a turtle turn a string into a picture?

The turtle carries a position and a heading angle. Reading the string left to right, F or G moves forward while drawing a line, + and - rotate the heading by a fixed angle δ, and [ / ] push and pop the turtle's state on a stack so a branch can split off and the main stem can resume exactly where it left off.

Why do real plants look so irregular if L-systems are so regular?

A stochastic L-system gives a symbol two or more alternative replacement rules and picks between them randomly, weighted by probability, each time it is applied. This breaks the mechanical regularity of a pure L-system and produces far more convincing, nature-like variation between branches, closer to how real ferns and trees actually look.

Try it live

Everything above runs in your browser — open Turtle Graphics & L-Systems and pick a preset (Dragon Curve, Koch Snowflake, Sierpiński, Hilbert Curve, Fractal Plant) or write your own axiom and rules. Nothing is installed, nothing is uploaded.

▶ Open Turtle Graphics simulation

What did you find?

Add reproduction steps (optional)