4. Turtle Drawing and Strings (L-Systems)

Quick Overview of Day

Turtle drawing by interpreting strings. L-Systems to generate strings. Draw interesting patterns using L-System strings.

4.1. Controlling a Turtle With Strings

By combining what we know about moving turtles on the screen, and what we have learned about traversing strings, we can now write a program that controls the movement of a turtle based on a string. For example, the string FF+F-FF might make a turtle:

  • move forward by some distance twice in a row

  • turn right by some angle

  • move forward again

  • turn left by some angle

  • move forward twice in a row

To turn a string like FF+F-FF into a set of instructions that our turtle can execute, we need to look at each character of the string, one at a time. We can do this easily with a for loop, as you can see in the following example.

4.1.1. Try This

In the code above, try:

  • changing the instruction string to have the turtle draw a different image

  • adding three additional possible instruction for the turtle. Any B in the string should be interpreted as an instruction to move backwards (by the same amount as F moves forward). A U should cause the turtle to pick up it’s pen (so it doesn’t draw as it moves). A D should cause the turtle should put down it’s pen (so that it draws as it moves).

  • increasing the speed of the turtle

After completing the tasks above, try having the turtle use the following instruction string: UBBBBBBDF-F++F-F-F-F++F-F++F-F++F-F-F-F++F-F

4.2. Turtles and Strings and L-Systems

This section describes a much more interesting example of string iteration and the accumulator pattern. Even though it seems like we are doing something that is much more complex, the basic processing is the same as was shown in the previous sections.

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.

The rules of an L-system are really a set of instructions for transforming one string into a new string. After a number of these string transformations are complete, the string contains a set of instructions. Our plan is to let these instructions direct a turtle as it draws a picture.

To begin, we will look at an example set of rules:

Axiom

A

Rule 1 Change A to B

A → B

Rule 2 Change B to AB

B → AB

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.

Now let’s look at these simple rules in action, starting with the string A:

A
B      Apply Rule 1  (A is replaced by B)
AB     Apply Rule 2  (B is replaced by AB)
BAB    Apply Rule 1 to A then Rule 2 to B
ABBAB  Apply Rule 2 to B, Rule 1 to A, and Rule 2 to B

Notice that each line represents a new transformation for entire string. Each character that matches a left-hand side of a rule in the original has been replaced by the corresponding right-hand side of that same rule. After doing the replacement for each character in the original, we have one transformation.

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.

Try running the example above with different values for the number_of_iterations parameter. You should see that for values 1, 2, 3, and 4, the strings generated follow the example above exactly.

One of the nice things about the program above is that if you want to implement a different set of rules, you don’t need to re-write the entire program. All you need to do is re-write the apply_rules function.

Note

Suppose you had the following rules:

Axiom

A

Rule 1 Change A to BAB

A → BAB

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

4.3. Drawing With L-Systems

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

Axiom

F

Rule 1

F → F-F++F-F

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 apply_rules function for this L-system.

def apply_rules(letter):
    """Apply rules to an individual letter, and return the result."""
    # Rule 1
    if letter == 'F':
        new_string = 'F-F++F-F'

    # no rules apply so keep the character
    else:
        new_string = letter

    return new_string

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. This is the string we used to draw a simple image at the start of this section! 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 draw_l_system 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 draw_l_system(some_turtle, instructions, angle, distance):
    for task in instructions:
        if task == 'F':
            some_turtle.forward(distance)
        elif task == 'B':
            some_turtle.backward(distance)
        elif task == '+':
            some_turtle.right(angle)
        elif task == '-':
            some_turtle.left(angle)

Here is the complete program, which combines generating the L-System string, and then using it to draw with the turtle.

Note

Try some different angles and segment lengths to see how the drawing changes. Start with 90 degrees, and experiment from there. You might want to use Thonny when experimenting, since Thonny makes it easy to end a program at any point. Using window.tracer(10) will also greatly speed up your programs.

4.4. Practice Problems

Adapt the template code given above to create drawings of other famous L-Systems.

4.4.1. Hilbert Curve

Use the following axiom and rules to create the Hilbert curve.

Angle

90 degrees

Axiom

L

Rule 1

L → +RF-LFL-FR+

Rule 2

R → -LF+RFR+FL-

4.4.2. Dragon Curve

Use the following axiom and rules to create the dragon curve.

Angle

90 degrees

Axiom

FX

Rule 1

X → X+YF+

Rule 2

Y → -FX-Y

4.4.3. Arrowhead Curve

Use the following axiom and rules to create the arrowhead curve.

Angle

60 degrees

Axiom

FX

Rule 1

X → YF+XF+Y

Rule 2

Y → XF-YF-X

4.4.4. Peano-Gosper Curve

Use the following axiom and rules to create the Peano-Gosper curve.

Angle

60 degrees

Axiom

FX

Rule 1

X → X+YF++YF-FX--FXFX-YF+

Rule 2

Y → -FX+YFYF++YF+FX--FX-Y

4.4.5. Sierpinski Triangle

Use the following axiom and rules to create the Sierpinski Triangle.

Angle

60 degrees

Axiom

FXF--FF--FF

Rule 1

F → FF

Rule 2

X → --FXF++FXF++FXF--

4.4.6. Snowflake

Use the following axiom and rules to create a snowflake shape.

Angle

72 degrees

Axiom

F-F-F-F-F

Rule 1

F → F-F++F+F-F-F

4.4.7. Unnamed Shape

If you know the name of this shape, please tell me!

Use the following axiom and rules to create an interesting shape.

Angle

45 degrees

Axiom

L--F--L--F

Rule 1

L → +R-F-R+

Rule 2

R → -L+F+L-

4.4.8. Making Your Own Shapes

Note

If you have experimented with all of the shapes above, and are thinking about creating your own, look for symmetry in the rules given above…

Next Section - 1. Using Lists