🐢 Turtle Graphics & L-Systems

An L-system (Lindenmayer system) is a parallel rewriting grammar invented by Aristid Lindenmayer in 1968. Starting from an axiom string and applying production rules iteratively, it generates complex strings that a turtle interprets as movement commands — F (forward), + (turn left), - (turn right), [ (push state), ] (pop state) — producing self-similar fractal structures of arbitrary complexity. 🇺🇦 Українська

Presets

L-System Rules

String length
Draw steps
Stack depth
Draw time

How L-systems Work

Each iteration applies all production rules simultaneously to every character: the string grows exponentially. At iteration 10 the Dragon Curve string is 210 = 1024 steps; at iteration 14 it's over 16 000. The key insight is self-similarity: every sub-segment of the Dragon Curve at level n looks like the whole curve at level n-1. Fractal dimension of the Dragon Curve is exactly 2 (it fills the plane); the Koch curve has dimension log(4)/log(3) ≈ 1.261; the Sierpiński triangle is log(3)/log(2) ≈ 1.585.

About this simulation

This simulator rewrites an axiom string using per-character production rules (F, X, Y — or A/B for Hilbert, G for Sierpiński) for a chosen number of iterations, then feeds the resulting string to a virtual turtle: F/G move forward and draw, +/- rotate by the angle setting, and [/] push and pop the turtle's position and heading onto a stack, enabling branching structures like the fractal plant and binary tree. Six presets — Dragon Curve, Koch Snowflake, Sierpiński, Hilbert Curve, Fractal Plant, and Binary Tree — load pre-tuned axioms, rules, and angles, but every field is editable so you can invent your own L-system.

🔬 What it shows

A live readout of exponential string growth: at 10 iterations the Dragon Curve already has 1,024 draw steps, and the app tracks string length, draw steps, maximum stack depth (branching depth), and draw time in milliseconds as you adjust settings.

🎮 How to use

Pick a preset button to load its axiom and rules, or type your own into the Axiom, Rule F, Rule X, and Rule Y fields. Drag the Angle slider (1°–180°) and Iterations slider (1–14) to reshape the curve, then hit ▶ Draw (or +/- Iter for quick steps). Toggle Rainbow (hue cycles across the path) or Mono (single purple stroke) coloring.

💡 Did you know?

The Dragon Curve has a fractal dimension of exactly 2 — meaning at high iteration counts it fills the plane — while the Koch Snowflake sits at log(4)/log(3) ≈ 1.261 and the Sierpiński Triangle at log(3)/log(2) ≈ 1.585.

Frequently asked questions

What do the F, +, -, [ and ] symbols actually do?

F (and G, used only in the Sierpiński preset) tells the turtle to move forward one unit while drawing a line. + rotates the turtle left by the Angle slider's value in degrees, and - rotates it right by the same amount. [ pushes the turtle's current x, y position and heading onto a stack; ] pops the last saved state back, snapping the turtle to where it branched from — this is exactly how the Fractal Plant and Binary Tree presets create side branches.

Why does the string get so long so fast?

Every iteration replaces every matching character with its full rule text at once (a parallel rewrite), so the string length grows roughly exponentially with the base set by the rule's expansion factor. The Dragon Curve preset reaches 1,024 draw steps at 10 iterations and can exceed 16,000 by iteration 14 — the code caps expansion at 2,000,000 characters as a safety limit.

What's the difference between the six presets?

Dragon Curve (axiom FX, 90° angle) and Koch Snowflake (axiom F--F--F, 60°) use single-line rewriting; Sierpiński (F-G-G, 120°) alternates F and G rules; Hilbert Curve (axiom A, 90°) uses paired A/B rules for a space-filling path; Fractal Plant (axiom X, 25°) and Binary Tree (axiom F, 20°) add [ and ] to create branching, leaf-like shapes instead of a single continuous curve.

What does Rainbow vs Mono color mode change?

Rainbow assigns each drawn segment a hue based on its position in the overall step sequence (0-360°), producing a gradient across the whole curve. Mono draws the entire path in a single purple stroke (#a78bfa) and, unlike Rainbow, breaks the canvas path at every branch point so segments render as one continuous stroke per branch.

Why do Iterations only go up to 14?

Because each iteration roughly doubles or triples the string length depending on the ruleset, going much higher would generate millions of characters and draw steps, slowing the browser to a crawl. The Iterations slider is capped at 14 and the string-building loop itself stops early if the rewritten string exceeds 2,000,000 characters.