CS 151: Project 7

Title image Project 7
Fall 2018

Project 7: Fractals and Trees

The assignment is to bring together the lsystem and turtle_interpreter pieces 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. Make it possible to draw trees using L-systems. To do that, we need to add code to turtle_interpreter.drawString to support branches. To draw branches, we need a mechanism for remembering the position and heading the turtle had before it starting the branch (i.e. where is the forking point of the branch?). We will add to symbols to our strings:
    	[ is "save the turtle's heading and position"
    	] is "restore the turtle's heading and position to the most recently saved heading and position"
    We will use a list data structure to store the heading and position. Each time we save, we add information to the end of the list (using append) and each time we restore, we remove information from the end of the list (using pop). Since this is making a list behave like a data structure called a stack, we will name the variable stack. That means, we should add code to the beginning of drawString to initialize the variable.
    	stack = []

    We then need to add two elif statents to the code that handles each character. Copy-paste the code-outline below and write the code.

            # 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
    and don't forget to update the docstring to include information about the newly supported characters:
        [ : save position and heading
        ] : restore position and heading

    Download systemB to test your updated code (run testlsystem.py with a distance of 5 and an angle of 22.5). Each tree should look like this:

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

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

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

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

Extension Examples

These are just examples, not necessarily recommendations. Please feel welcome and encouraged to design your own. A great extension genuinely interests you, pushes your understanding of CS, and inspires you to learn something new.

Hand-in and Report

  1. Put the python files you wrote on the Courses server in your private directory in a folder named Project7.
  2. Make a new wiki page for your assignment. Put the label cs151f18project7 on the page.

    In general, your report should follow the outline below.

    • Title includes your name and clearly describes the project.
    • Section headings are used to delineate distinct sections of the report.
    • Abstract identifies key lecture concepts (e.g. code structres, data types, and libraries) relevant to the project.
    • Abstract explains why key lecture concepts are important to achieving project goals.
    • Abstract identifies program output(s), giving context to the project tasks.
    • Solutions to tasks are described, focusing on how you used key lecture concepts to solve each task.
    • Required images/outputs are present and clearly labeled.
    • Reflection at the end of the report addresses how the lecture concepts mentioned in the abstract made this project possible. If you can think of a more elegant way to achieve the same results, please share!
    • Sources, imported libraries, and collaborators are cited, or a note is included indicating that none were referenced.
    • Don't forget to label your writeup so that it is easy for others to find. For this project, use cs151f18project7