In 1968 Aristid Lindenmayer, a biologist, invented a formal system that provides a mathematical description of plant growth known as an **L-system**. L-systems were designed to model the growth of biological systems. You can think of L-systems as containing the instructions for how a single cell can grow into a complex organism. L-systems can be used to specify the **rules** for all kinds of interesting patterns. In our case, we are going to use them to specify the rules for drawing pictures. Here is an example from a research project I am working on: Fractel generator

The rules are rewrite rules, rules for taking a string from one form to the next.

Consiter the following rules:

K | Axiom (starting point) |

K->C | Rule 1 - Change a K to a C |

C->KC | Rule 2 - CHange a C to a KC |

left hand side -> right hand side

where the left hand side is a single symbol and the right hand side is a sequence of symbols. You can think of both sides as being simple strings. The way the rules are used is to replace occurrences of the left hand side with the corresponding right hand side.

K C Apply Rule 1 (K is replaced by C) KC Apply Rule 2 (C is replaced by KC) CKC Apply Rule 1 to K then Rule 2 to C KCCKC Apply Rule 2 to C, Rule 1 to K, and Rule 2 to C

So how would we encode these rules in a Python program? There are a couple of very important things to note here:

- Rules are very much like if statements.
- We are going to start with a string and iterate over each of its characters.
- As we apply the rules to one string we leave that string alone and create a brand new string using the accumulator pattern. When we are all done with the original we replace it with the new string.

The number we pass to print(createLSystem(2, "K")) is the number of times the ruke set is run across the system.

Can we make to loop to call the system 4 times with the number equal to 1,2,3,4?

Can you makeup and try some other rules?

Suppose you had the following rules:

A | Axiom |

A -> BAB | Rule 1 Change A to BAB |

Now let’s look at a real L-system that implements a famous drawing. This L-system has just two rules:

F | Axiom |

F -> F-F++F-F | Rule 1 |

F | Go forward by some number of units |

B | Go backward by some number of units |

- | Turn left by some degrees |

+ | Turn right by some degrees |

- F
- F-F++F-F
- F-F++F-F-F-F++F-F++F-F++F-F-F-F++F-F

defapplyRules(ch): newstr =""ifch =='F': newstr ='F-F++F-F'# Rule 1else: newstr = ch# no rules apply so keep the characterreturnnewstr

Pretty simple so far. As you can imagine this string will get pretty long with a few applications of the rules. You might try to expand the string a couple of times on your own just to see.

The last step is to take the final string and turn it into a picture. Let’s assume that we are always going to go forward or backward by 5 units. In addition we will also assume that when the turtle turns left or right we’ll turn by 60 degrees. Now look at the string F-F++F-F. You might try to use the explanation above to show the resulting picture that this simple string represents. At this point its not a very exciting drawing, but once we expand it a few times it will get a lot more interesting.

To create a Python function to draw a string we will write a function called drawLsystem The function will take four parameters:

- A turtle to do the drawing
- An expanded string that contains the results of expanding the rules above.
- An angle to turn
- A distance to move forward or backward

defdrawLsystem(aTurtle,instructions,angle,distance):forcmdininstructions:ifcmd =='F': aTurtle.forward(distance)elifcmd =='B': aTurtle.backward(distance)elifcmd =='+': aTurtle.right(angle)elifcmd =='-': aTurtle.left(angle)

Here is the complete program in activecode. The main function first creates the L-system string and then it creates a turtle and passes it and the string to the drawing function.

What is going on here?

Can you create some interesting fractels by varying the rules, the distances, the angles.

You can use additional characters, for example F could mean forward 5, and G could mean forward 10. You should also play around with moving backwards.

Can you think of a way to make the kind of fractel I make here (for extra credit toward your midterm).

- A link to your repl.it
- Your best picture

Edit | Attach | ~~Watch~~ | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions

Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback