Skip to content
Sample of Lindenmayer L-System (Textbook Formulation)

shiffman_pearson_textbook · lindenmayerTextbook

Lindenmayer L-System (Textbook Formulation)

Lindenmayer L-system string rewriting with a 2-D turtle interpreter, the canonical textbook formulation used in both Shiffman's Nature of Code Ch. 8 and Pearson's Generative Art. An axiom is rewritten using deterministic or stochastic production rules for `iterations` generations, then interpreted character-by-character: F = draw forward, f = move (no draw), +/- = rotate, [ and ] = push/pop (x, y, heading). Six named presets: 'algae' (Lindenmayer's original A -> AB, B -> A), 'cantor' (L-system version of the Cantor set), 'koch_curve', 'koch_snowflake', 'fractal_plant' (Shiffman's canonical X -> F+[[X]-X]-F[-FX]+X, F -> FF), and 'stochastic_plant' with randomly-picked rules at each expansion. step(n) advances iterations by n and re-draws, producing a growth animation.

Original: https://natureofcode.com/fractals/

▶ Try the live demo (JS port) ▶ Run reference.py via Pyodide 10 formulas have instant JS ports. The Pyodide path runs the actual Python in your browser — first load is ~6 MB, then instant.

Family
shiffman_pearson_textbook
Source
shiffman · pearson
Evolution
lindenmayer_string_rewriting
Render
turtle_stroked_lines
Complexity
medium
Agents
1
Year
1968
Author
Aristid Lindenmayer (1968); Daniel Shiffman (Nature of Code Ch. 8 L-systems, 2012); Matt Pearson (Generative Art, 2011)

Math essence

Iterative string rewriting: for each character in current string, look up its rule (deterministic) or randomly pick one (stochastic) and concatenate. Turtle: heading rotates by +/- angle_deg per + or -; F draws a segment from (x, y) to (x + L*cos(h), y + L*sin(h)); [ pushes the state, ] pops it. Segments are rasterised onto the canvas with a 5-pixel cross stamp and a low-alpha blend. Step length decays geometrically with iterations so the drawing fits the canvas.

Parameters

Name Type Default Range Description
dim int 600 200–1400
preset str fractal_plant — Named preset; axiom/rules/angle overridden by explicit args.
axiom str null — Starting string; if None use preset.
rules dict null — Production rules {'A': 'AB', ...}; if None use preset.
angle_deg float 25 1–120
iterations int 5 1–9 Rule applications; step(n) advances by n (clamped to 9).
step_len float 6 0.5–30 Turtle forward step length in pixels.
stochastic bool false — Pick from stochastic_rules uniformly at random.
stochastic_rules dict null — {'F': ['F[+F]F', 'F[-F]F', ...]}.
ink_alpha float 0.85 0.1–1
bg_color tuple 252,248,240 —
ink_color tuple 35,60,40 —
seed int null 0–999999

Reference

First 60 lines of reference.py. Full source: library_external/shiffman_pearson_textbook/lindenmayerTextbook/reference.py

from __future__ import annotations

from typing import ClassVar

import numpy as np


PRESETS: dict[str, dict] = {
    "algae": {
        "axiom": "A",
        "rules": {"A": "AB", "B": "A"},
        "angle_deg": 0.0,
        "iterations": 7,
        "draw_alphabet": "AB",
        "step_len": 0.0,
        "start_angle_deg": 0.0,
        "start_pos": (0.5, 0.95),
    },
    "cantor": {
        "axiom": "A",
        "rules": {"A": "ABA", "B": "BBB"},
        "angle_deg": 0.0,
        "iterations": 5,
        "draw_alphabet": "AB",
        "step_len": 0.0,
        "start_angle_deg": 0.0,
        "start_pos": (0.05, 0.5),
    },
    "koch_curve": {
        "axiom": "F",
        "rules": {"F": "F+F-F-F+F"},
        "angle_deg": 90.0,
        "iterations": 4,
        "draw_alphabet": "F+-",
        "step_len": 9.0,
        "start_angle_deg": 0.0,
        "start_pos": (0.05, 0.85),
    },
    "koch_snowflake": {
        "axiom": "F++F++F",
        "rules": {"F": "F-F++F-F"},
        "angle_deg": 60.0,
        "iterations": 4,
        "draw_alphabet": "F+-",
        "step_len": 7.0,
        "start_angle_deg": 0.0,
        "start_pos": (0.18, 0.65),
    },
    "fractal_plant": {
        "axiom": "X",
        "rules": {"X": "F+[[X]-X]-F[-FX]+X", "F": "FF"},
        "angle_deg": 25.0,
        "iterations": 5,
        "draw_alphabet": "F+-[]X",
        "step_len": 5.5,
        "start_angle_deg": -90.0,
        "start_pos": (0.5, 0.97),
    },
    "stochastic_plant": {
        "axiom": "F",

Try running it locally

The reference implementation is self-contained Python with numpy. Install the package and run:

python library_external/shiffman_pearson_textbook/lindenmayerTextbook/reference.py

Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.

More from shiffman · pearson

See all shiffman_pearson_textbook artworks →