CS 151: Project 7

Title image Project 7
Spring 2020

Project 7: Fractals and L-systems

The assignment is to build an interpreter that converts strings into turtle graphics drawings. The goal is to make a scene that consists of fractal shapes, trees, and other turtle graphics (think back to projects 1, 2 and 3). Your top-level program will include both the lsystem and turtle_interpreter modules.


  1. Setup

    Create a new file called turtle_interpreter.py. Put your name and date at the top in comments. The purpose of this file is to convert a string into an image using simple turtle commands.

    The primary function in this file is drawString. The drawString function is an interpreter. It converts information in one form into information in another form. In this case, it converts a string of characters into a series of turtle commands.

  2. Write a drawString method

    The drawString method takes in a string, a distance, and an angle as arguments. It should loop over the characters in the string and execute the turtle command specified by each character. The following are the actions for each character.

    FMove the turtle forward by distance
    +Turn the turtle left by angle
    -Turn the turtle right by angle
    [Append the turtle's heading and position to a list (stack)
    ]Pop the turtle's heading and position from the list (stack) and restore the turtle's state

    Video 2: Storing and restoring the turtle state

    + (more detail)

    The overall structure of the drawString method is given by the following algorithm.

    def drawString( dstring, distance, angle ):
        """ Interpret the characters in string dstring as a series
        of turtle commands. Distance specifies the distance
        to travel for each forward command. Angle specifies the
        angle (in degrees) for each right or left command. The list of 
        turtle supported turtle commands is:
        F : forward
        - : turn right
        + : turn left
        [ : save the turtle's heading and position
        ] : restore the turtle's heading and position
        # assign to stack the empty list
        # for each character c in dstring
            # if c is equal to 'F'
                  # tell the turtle go forward by distance
            # else if c is equal to '-'
                  # tell the turtle to turn right by angle
            # else if c is equal to '+'
                  # tell the turtle to turn left by angle
            # else if c is equal to '['
                  # append to stack the position of the turtle (position method)
                  # append to stack the heading of the turtle (heading method)
            # else if c is equal to ']'
                  # tell the turtle to pick up pen
                  # call the setheading method of the turtle and pass it the value popped off stack
                  # call the goto method of the turtle and pass it the value popped off stack
                  # tell the turtle to put down pen
        # call turtle.update() (not in the for loop)

    Use a list data structure to store the heading and position. For each [ character, add information to the end of the list using append. For each ] character, remove the position and heading information from the end of the list using pop. This makes a list behave like a data structure called a stack, so a good name for it is stack.

  3. Add a hold function

    The function hold() is given below. It sets up the turtle window to quit if you type 'q' or click in the window. Copy it to your turtle_interpreter.py file.

    Note: if you have import turtle as t then you need to change all of the instances of "turtle." to "t." in the code below.

    def hold():
        '''Holds the screen open until user clicks or presses 'q' key'''
        # Hide the turtle cursor and update the screen
        # Close the window when users presses the 'q' key
        turtle.onkey(turtle.bye, 'q')
        # Listen for the q button press event
        # Have the turtle listen for a click
  4. Test drawString

    Each of the following files tests a different aspect of the drawString function. Download and run each one, making sure the output is correct.

    • test7-1.py - draws a blue triangle, a red square, and a green pentagon.
    • test7-2.py - draws a symmetric tree with left and right branches that each have left and right branches. The tree should be in a square pot with legs.
    • test7-3.py - draws a single L-system given the L-system file, distance, and angle. It uses 3 for the number of iterations. For example run it with the following command line arguments:

      python3 test7-3.py systemA1.txt 20 90

      python3 test7-3.py systemA2.txt 10 90

    • test7-4.py - uses your L-system class to create several complex trees. Download systemB.txt and run it with the command:

      python3 test7-4.py systemB.txt 10 22.5

      The result should look like this. You can also use this test function with the other L-system files.

  5. Create an abstract scene

    Video 3: Creating an Abstract Scene

    Create a file called abstract.py. The file will need to import sys, turtle, lsystem, and turtle_interpreter. Write a function that creates an abstract image that includes three L-systems. The phrase "abstract image" just means it does not have to make any attempt to be realistic. The image should be constructed to take advantage of your Python programming skills--don't rely wholly on the random package and a loop. Your goal should be visual complexity combined with order, and simplicity in your code. One idea is to make an interesting pattern by drawing the same L-system in different positions. Using a hierarchy of functions is also a powerful tool.

    Include at least three different L-systems in your image, with at least one of them using brackets. Don't feel beholden to use the suggested number of iterations or angles for any L-system. You can get the filenames for the L-system files from the command line, by asking the user for them, or by hard-coding them into your code.

    In your image function, you can use turtle commands to pick up the pen, move it to a new location, change colors, change pen widths, and put down the pen before drawing a new shape. Look at the test file above for inspiration.

    A picture with 3 different L-systems is required image 1.

  6. Make a grid of L-system trees

    Make a new file grid.py that contains a function that draws a set of 9 trees based on the systemB L-system, or some variation of it that has brackets. Order the 9 trees as a 3x3 grid. From left to right the number of iterations of the L-system should go from 1 to 3. From top to bottom, the angle of the L-system should be 22, 46, and 60. Use a double for-loop to create the grid.

    A picture with a grid of L-systems is required image 2.

  7. Make a scene that includes L-systems

    Make a new file scene.py that makes a non-abstract scene with two or more objects generated using L-systems. Include in your scene at least one new L-system with brackets (e.g. a tree) that you haven't used yet. You can use one of the L-systems from ABOP (look at pages 9, 10, and 25 for single-rule L-systems) or make up one of your own. The scene does not need to be complex, but your code should exhibit modularity and good design.

    A non-abstract scene that includes 2 different L-systems is required image 3.

Follow-up Questions

  1. What is your CS understanding of an interpreter?
  2. What is an L-system (keep it brief)?
  3. What does it mean to loop over the characters in a string?
  4. What is the purpose of using a stack in drawString?
  5. Can you think of a movie where you were impressed by the computer generated trees?


Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own. Be sure to describe any extensions you complete in your report. Include pictures.

Submit your code

Turn in your code (all files ending with .py) by putting it the appropriate directory in the Courses server. You can access Courses through a browser at vpn.colby.edu. On the Courses server, you should have access to a directory called CS151, within that, a directory with your user name, and within that a directory named Private. Inside the Private directory there is a Projects directory with sub-directories for each week's project where you should submit your code. Please submit only code that you want to be graded.

When submitting your code, double check the following.

  1. Is your name at the top of each code file?
  2. Does every function have a comment or docstring specifying what it does?
  3. Is your handin project directory inside your Private folder on Courses?

Write Your Project Report

If you haven't already made a new page for this report on the wiki, then make one now (Log into the wiki, goto your Personal space by selecting "Personal Space" on the menu under the Person icon, then make the page using the "Create" button. Put the label cs15120project7 in the label field on the bottom of the page. But give the page a meaningful title.

Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it.

Your project report should contain the following elements. Please include a header for each section.