CS 151: Project 7

Title image Project 7
Fall 2019

Project 7: Fractals and Trees

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

    + (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.

    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 functin. Download and run each one, making sure the output is correct.

    • makeShapes.py - this test code should draw a triangle, a square, and a pentagon.
    • makeTree.py - this test code should draw a symmetric tree with left and right branches that each have left and right branches. The tree should be in a pot.
    • testsimple.py - this test code draws a single L-system given the L-system file, distance, and angle. It uses 3 for the number of iterations. For example:

      python3 testsimple.py systemA1.txt 20 90

      python3 testsimple.py systemA2.txt 10 90

    • testlsystem.py - this test code should use your L-system class to create several complex trees. You will also need systemB.txt. Run this with the command:

      python3 testlsystem.py systemB.txt 10 22.5

      You can also use this test function with the other L-system files.

  5. Create 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 using L-systems. This 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 complexity, yet order in your image 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.

    Your image should include at least three different L-systems, 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 testlsystem.py 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. The scene must include 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 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?


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 in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, a directory with your user name. Within this directory is a directory named private. Files that you put into that private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, create a new directory, such as project1, and then copy your code into the project directory for that week. 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 cs151f19project7 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.