Playground
L-systems: plants and fractal curves

Plants from rewrite rules

L-system An L-system starts with a short text, the axiom. In every round, each symbol is replaced at the same time by the text of its rule. After a few rounds the text is long and has a pattern inside a pattern, like a fractal or a branching plant. Aristid Lindenmayer invented it in 1968 to describe how plant cells grow. Wikipedia

Turtle graphics A turtle reads the final text symbol by symbol and draws: it steps forward, turns by the angle, or jumps back to a saved place at a bracket. The picture is scaled to fit the canvas. Wikipedia

Original work

How it works

The rule text is checked first, then the text grows round by round, then the turtle draws it.

  • All symbols are replaced at the same time, so a rule never sees the result of another rule in the same round.
  • F and G draw a line of one step, f moves without a line, + turns left, - turns right. Other letters draw nothing.
  • A bracket [ saves the position and direction on a stack, ] takes them back. This makes branches.
  • The bounds of all lines give the scale and the offset, so every picture fits the canvas.

Source code:l-system-grammar.tsl-system-turtle.ts

Note Simplifications in this demo:

  • Only deterministic, context-free rules. No random and no parametric rules.
  • 2D only, with one line width and one color.
  • Limits: axiom 64 characters, rules 600 characters, at most 8 rules, 64 symbols per right side, 12 iterations and 200 000 symbols in total.

F, G: draw a step. f: move without a line. + / -: turn left / right. [ ]: save / restore the position. Other letters A-Z only help the rules. One rule per line, for example F=F+F-F.

Angle (degrees)
Start direction (degrees)
Iterations
Symbols: 6,263Lines: 1,488