Tags: %TAGME{ tpaction="" web="Main" tag="" }% view all tags

Turtles and Strings and L-Systems - Fractels

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

Each rule set contains an axiom which represents the starting point in the transformations that will follow. The rules are of the form:

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.

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:

  1. Rules are very much like if statements.
  2. We are going to start with a string and iterate over each of its characters.
  3. 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.

Letís look at a simple Python program that implements the example set of rules described above: LSystemExample1

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

What kind of a string would these rules create? Modify the program above to implement the rule.

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

This L-system uses symbols that will have special meaning when we use them later with the turtle to draw a picture.

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

Here is the applyRules function for this L-system. Let's try a few on the board. Let degrees = 60, and length be 5.

  1. F
  2. F-F++F-F
  3. F-F++F-F-F-F++F-F++F-F++F-F-F-F++F-F
def applyRules(ch):
    newstr = ""
    if ch == 'F':
        newstr = 'F-F++F-F'   # Rule 1
        newstr = ch    # no rules apply so keep the character

    return newstr

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

def drawLsystem(aTurtle,instructions,angle,distance):
    for cmd in instructions:
        if cmd == 'F':
        elif cmd == 'B':
        elif cmd == '+':
        elif cmd == '-':

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.

Turtle Code to draw L-system

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).


1. Sierpinski triangle

  • variables : F G
  • constants : + −
  • start : F−G−G
  • rules : (F → F-G+F+G-F ), (G → GG)
  • angle : 120į
Here, F and G both mean "draw forward", + means "turn left by angle", and − means "turn right by angle".

2. Sierpiński arrowhead curve

  • variables : A B
  • constants : + −
  • start : A
  • rules : (A → B−A−B), (B → A+B+A)
  • angle : 60į
Here, A and B both mean "draw forward", + means "turn left by angle", and − means "turn right by angle"
For extra credit turn in:
  1. A link to your repl.it
  2. Your best picture
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2020-02-18 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback