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.
| Order | Result |
|---|---|
| 0 | F |
| 1 | F+F-F |
| 2 | F+F-F+F+F-F-F+F-F |
| 3 | 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-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
The result is a sweet looking Dragon Curve.
