l-systems in Python

An l-system or Lindenmayer system is a set of replacement rules used to model, among other things, plant growth. Starting with an axiom or initial state, and one or more replacement rules, we take each symbol in the axiom and apply the rules to them. So for instance:

  • Axiom: F
  • Rule: F -> F+F-F

The letter F is our variable. Both + and - are constants (we'll worry about what they mean in a bit...)
So to apply our rule, we replace F with F+F-F. We then repeat.

OrderResult
0F
1F+F-F
2F+F-F+F+F-F-F+F-F
3F+F-F+F+F-F-F+F-F+F+F-F+F+F-F-F+F-F-F+F-F+F+F-F-F+F-F

So we end up with a long, goofy string. The cool factor comes in when we do something with the symbols. If we treat the string as Logo-like turtle graphics commands, we can draw the resulting figure.

In the above example, we say that F mean 'move forward x spaces'. The + will mean 'turn clockwise Θ degrees' and the - will mean 'turn counterclockwise Θ degrees. Finally we say Θ is 60 and x is 5

Tetra Dragon


The result is a sweet looking Dragon Curve.






Next >>


You ever hear of the neutron bomb? Destroys people - leaves buildings standing. Fits in a suitcase. It's so small, no one knows it's there until - BLAMMO. Eyes melt, skin explodes, everybody dead. So immoral, working on the thing can drive you mad. That's what happened to this friend of mine. So he had a lobotomy. Now he's well again.

J Frank Parnell