CS 151: Forests of Trees

Forests of Trees

The purpose of this project is to give you more practice working with classes. You'll complete a class that handles L-systems and then write a program that uses the class to generate shapes. The class will hold all of the information required by an L-system and be able to read and write that information to a file. The overall purpose is to package together all of the things required to build an L-system string and make them easy to manipulate.


  1. The first task is to download lsystem2.py. The file contains a skeleton LSystem class with some of the methods already written. Scan through the methods and make sure you understand what they're doing. In particular, look at the set method and identify each field's name and what information it stores. Make a box diagram of the object and its fields. Feel free to change the default L-system to one that you like.

    One of the important things to note is how the rules are stored. Each rule is a list with two elements. The first element is the symbol that will be replaced. The second element is the replacement value. The rules field itself is a list of rules, or list of 2-element lists.

  2. Next, create a test function at the bottom. Call it whatever you like. I called mine lsystemtest(). At the bottom of the file, you want to call your test function if the file was called from the command line. For example, for my test function:
    if __name__ == '__main__':
  3. The test function should first create an object of type LSystem. The following code, for example, creates an object of type LSystem and puts a reference to it into the variable ls

    ls = LSystem()

    After creating the object, you need to use it to test out all of the methods in the class.

  4. Write the methods that don't yet have code. As you complete a method, put a test for it in the test function. For example, the setDistance() and getDistance() you can test by first setting a value, then getting a value and testing whether they are the same by printing out both the value sent to setDistance and the value returned by getDistance.

    Note that the LSystem class does not know anything about drawing itself on the screen. The LSystem class just produces a string on demand. The python script parse.py is a fully functional program that uses the LSystem class to create and draw three trees, moving the turtle in between the trees. Use the parse.py function to ensure you have working methods. The parse.py output is shown below. Note that each run will be different because of randomness in the angles.

  5. The final task is to write a completely new program that uses the LSystem class to generate a scene with at least a half-dozen tree-like things in it. Use at least three different tree-like L-systems in your image. You can use any of the tree-like L-systems from ABOP, modify them, or create your own. You are free to copy my processString function from parse.py to your new file in order to do the actual drawing, or you can use your own from lab 5, or you can write a new function that converts the L-system string into drawing commands, possibly using the new ZTurtle class.

    For interesting effects, try changing the distance or angle on the LSystem definition so that some trees look far away and some look close. Mixing your scene with other things you can draw with a turtle is encouraged (make it interesting, please). You can also try creating several different versions of a scene with something moving in it. The turtle.reset() function clears the screen and puts the turtle back to the center of the screen.


The following are suggested extensions.

  1. Make use of lists, loops and other control structures in your final image to make the code as compact as possible.
  2. Use your new ZTurtle class to create the trees.
  3. Create new L-systems that make interesting trees.
  4. Pick anything from ABOP to augment the L-systems and run with it.
  5. Generate a sequence of images where some aspect of the scene changes so that you can generate an animated sequence.


Create a web page for your writeup. Your writeup should include the following.

Handing in

When your writeup is ready for viewing and linked to your index page, send me an email.

To hand in your python code, place it on the Academics fileserver in the private folder. Do not post it on the web. You may include snippets from your code in your writeup to demonstrate how certain aspects of the function work, but please do not include the whole thing.

To place the code on the fileserver, go to the server fileserver1 and select the Academics volume. Within that there ought to be a CS151 folder, and within that there should be a folder with your username, and within that ought to be a folder labeled private. You can read and write files to the private directory and I can read them, but no one else can access them. Put your python files in that directory to hand them in. Please organize your files into folders by project.