CS 151: Project #8

Project 8: Better Trees

Main course page

As with last week, the assignment is to bring together the lsystem and interpreter classes to make a scene that consists of fractal shapes and trees. Your top-level program will include both the lsystem and interpreter modules. Unlike last week, however, your scene.py may not include the turtle module or make any calls to turtle commands. If you want to draw something, you have to pass a string to the interpreter drawString function. There will be no exceptions to that rule. (Note that you may attach arbitrary meaning to any character not used by an L-system grammar.)


  1. In the Lsystem class create a method def __str__(self) that returns a nicely formatted string that might look like the following when printed.

    base X
    rule X -> F-[[X]+X]+F[+FX]-Xl
    rule F -> FF

    The __str__ function gets called automatically when an object is printed out. It's also called to implement casting an object to a string using str(thing). Note that your __str__ function should not print anything out. It should build a string from the information in your L-system and return the string. Remember that '\n' is how you create a newline character.

    In the test main function the line:

    print lsys

    should print out your nicely formatted string instead of the generic class instance string when you run your lsystem.py file.

  2. We want to be able to change the color of elements in an Lsystem without affecting the color of other elements. We would also like to avoid setting colors, widths, or other turtle drawing features in drawString. For task 2, modify your drawString function so that it supports four additional characters in the strings.
    • '<' - the left angle bracket should push the current turtle color onto a color stack. You'll need to create a separate colorstack variable similar to the one used for position and heading. You can use the function turtle.color() to get the turtle's current color. Note that this function returns a duple of colors. You should append just the first element of the duple onto the color stack (e.g. turtle.color()[0] ).
    • '>' - the right angle bracket should pop the current turtle color off the color stack and set the turtle's color to that value.
    • 'g' - set the turtle's color to green (e.g. (0.15, 0.5, 0.2) ).
    • 'y' - set the turtle's color to light yellow ( e.g. (0.8, 0.8, 0.3) ).
    • 'r' - set the turtle's color to red ( e.g. (0.7, 0.2, 0.3) ).

    Once you have made the modifications to drawString, try running systemFL using the classtest.py test function from the lab. You can also use the test function single.py to draw just a single tree.

  3. Create a file task1.py. Have the file import lsystem and interpreter (the new versions). Make a function that creates a simple image with at least two different multi-rule L-systems. See if you can draw the base string and rule below the corresponding picture.

    This is required image 1.

  4. Create a file task2.py and write a function that makes a forest or garden of flowers that includes L-systems with leaves or other ornaments that make use of the color store/restore. The following are variations with leaves and flowers, all defined by strings (no special characters except L). Try to include L-system trees with different numbers of iterations.
    systemCL systemDL systemEL systemFL systemGL

    This is required image 2.

  5. Make two new L-systems. They can be variations on one of the ones provide, and L-system from the ABOP book, or one you create ones on your own. The L-systems of interest are given in the ABOP book chapter 1, pages 10, 11, and 25.

    If you use a variation, include both the original and your variant in your writeup. (The difference does not have to be large.)

    In the file task3.py, create a scene function that makes an image using your L-systems after 2, 3, and 4 iterations. Note, if using a different pattern of iterations is more interesting (e.g. 4, 5, 6) you are free to use a different set of three iteration values. Indicate what you used in your writeup.

    This is required image 3.



Make a new wiki page for your assignment. Give the page a useful title using English words. Put the label cs151f12project8 in the label field at the bottom of the page. Each of you needs to make your own writeup.

In addition to making the wiki page writeup, put the python files you wrote on the Courses server in your private handin directory.

Colby Wiki

In general, your writeup should follow the outline below.