L-System
First appeared 1968 · Aristid Lindenmayer — Introduced by Aristid Lindenmayer in 1968 to model plant development; generalised for graphics in the 1990 book. · timeline ↗
third-party / eponym Name — "L-system" is an eponym for Aristid Lindenmayer (the "L"); Lindenmayer introduced the formalism in 1968 as developmental systems, and the shorthand was fixed by the later formal-language / graphics community.
In one line A string-rewriting grammar (Lindenmayer system) that repeatedly replaces symbols by rules to grow a self-similar string, then interprets that string as turtle graphics. The classic use is branching plant structures.
The code
The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.
l-system/index.html
const SEED = Math.floor(Math.random()*1e9);
function setup() {
randomSeed(SEED);
const s = Math.min(windowWidth, windowHeight);
createCanvas(s, s);
background('#f5f1e8');
noFill();
stroke('#2c3a26');
strokeCap(ROUND);
// Classical stochastic L-system from Lindenmayer for a 3D-ish plant:
// axiom: X
// rules: X → F+[[X]-X]-F[-FX]+X (or one of several variants)
// F → FF
// alphabet: F (forward), +/- (rotate), [/] (push/pop turtle state)
const RULES = random([
{ X: 'F+[[X]-X]-F[-FX]+X', F: 'FF' },
{ X: 'F[+X]F[-X]+X', F: 'FF' },
{ X: 'F-[[X]+X]+F[+FX]-X', F: 'FF' },
]);
const ANGLE = random(20, 28) * Math.PI / 180;
let str = 'X';
const iters = 5;
for (let i = 0; i < iters; i++) {
let next = '';
for (const ch of str) next += RULES[ch] ?? ch;
str = next;
}
const step = s * 0.014;
strokeWeight(s * 0.0017);
// Turtle starts at the bottom center, heading up (−Y).
let x = width / 2;
let y = height * 0.98;
let theta = -Math.PI / 2;
const stack = [];
for (const ch of str) {
if (ch === 'F') {
const nx = x + Math.cos(theta) * step;
const ny = y + Math.sin(theta) * step;
line(x, y, nx, ny);
x = nx; y = ny;
} else if (ch === '+') {
theta += ANGLE + (Math.random() - 0.5) * 0.04;
} else if (ch === '-') {
theta -= ANGLE + (Math.random() - 0.5) * 0.04;
} else if (ch === '[') {
stack.push([x, y, theta]);
} else if (ch === ']') {
[x, y, theta] = stack.pop();
}
}
}How it works
Define an alphabet of symbols, an axiom (starting string), and production rules that replace symbols. Generate, then draw: 1. Rewrite: starting from the axiom, simultaneously replace every symbol using the rules; repeat for n generations (the string grows, often exponentially). 2. Interpret as turtle graphics: F = move forward drawing, + / − = turn by a fixed angle, and [ / ] = push / pop the turtle's state (enabling branches). For example axiom F with rule F→F[+F]F[−F]F yields a bushy plant after a few iterations. Stochastic rules (random rule choice) add variation; parametric and context-sensitive L-systems model growth, leaf venation and development.
Parameters
axiom: string — The initial string the rewriting starts from.rules: string — Production rules that replace each symbol on every pass.iterations: number — Number of rewriting passes; structure grows exponentially in this value.angle: number — Turn angle applied when the turtle interprets + / − symbols.stepLength: number — Forward distance drawn per F symbol.
Lineage
Frequently combined with
- Genetic Algorithm (Evolutionary Art) · 1 work
Works using this algorithm (2)
References
- Mathematical Models for Cellular Interaction in Development, Parts I and II
- The Algorithmic Beauty of Plants
- The Nature of Code: Simulating Natural Systems with Processing
Last updated 2026-07-16