# L-Systems

An L-System, or Lindenmayer System is a simple but powerful image drawing technique that employs "turtle" graphics.

The recursive nature of the L-system rules leads to self-similarity and thereby fractal-like forms. The forms produced via these rules 'grow' and becomes more complex by increasing the iteration level of the form.

### Using an L-System to Create a Drawing

Imagine that a "turtle" with a pen in its tail is placed on the screen.

Based on the characters seen in a given "command" string, the turtle will perform different actions.

As the characters of this command string are examined from left to right, if an 'F' or 'G' is encountered, the turtle will take a step forward by some fixed amount (drawing a line behind it with its pen).

If instead, a '-' or '+' is encountered, the turtle turns by some fixed number of degrees left or right.

If any other letter is encountered, the turtle does nothing.

So for example, if the fixed number of degrees to turn is 90, the fixed distance to step forward is 10, and the command string was "F-F-F-F", the turtle would draw a square with perimeter 40.

However, in an L-System, we actually make things more interesting by starting with a "seed" string of commands, and repeatedly construct a new command string by replacing certain characters in accordance with some fixed rules.

For example, suppose we used the rule

'F'->"F-F++F-F"

If we started with a seed command string of

"F++F++F",

then after one round of replacements, we arrive at the new command string of

"F-F++F-F++F-F++F-F++F-F++F-F"

After a second round of replacements, we would have

"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-F-F-F++F-F++F-F++F-F-F-F++F-F++F-F++F-F-F-F++F-F"

As one can see, the length of the command string grows exponentially with the number of rounds of replacements, so one does not want to do this too frequently. Still, even after a couple of rounds the command string can result in a very interesting drawing by the turtle. To see the picture resulting from using this particular rule and seed with a "depth" of 5 rounds of replacements, compile and run the starter code and select "Koch" from the combo box before clicking the "Draw" button.