Colby across Johnson Pond
CS 151: Computational Thinking
(Spring 2014)

Syllabus

Teachers
Beatrice
Caitlin
Stephen
Dan
Lanya
Victoria
Matt
Hieu
Jacob
Scott
Hieu


Assignments


Other Pages

Project 8:
Better Trees


Assigned: Wed Apr 09 2014
Due: 11:59:59 PM on Tue Apr 15 2014
Team Size: 1
Language: Python


Lab:

Classes

The purpose of this lab is to give you practice in creating your own classes. In particular, we will convert both the lsystem and the turtle interpreter modules into classes. Ultimately, this will make it easier to build a more complex system.

If you have not already done so, take a look at the book 'The Algorithmic Beauty of Plants', which is the basis for this series of lab exercises. You can download the entire book from the algorithmic botany site

The other piece we'll be implementing this week is handling multi-rule L-systems. Adding this capability will enable you to draw more complex L-system structures.


Tasks

Most of the exercises in lab will involve building up an L-system class. This week it will be important for you to use the method names provided. The next several labs will expect the L-system and TurtleInterpreter classes to have certain methods with particular names.

  1. Create a new working folder and a new lsystem.py file. Label this one as version 2. The lsystem.py file will hold the LSystem class and a test function for it.
  2. Begin a class called LSystem. A class has a simple structure. It begins with the class declaration, and all methods are indented relative to the class definition, similar to a function.

    class LSystem(object):

    Just like with functions and methods, classes should have their own docstring. Each LSystem object will have two fields: base, representing the base string, and rules, representing the way replacements are made. Add a docstring for your class that describes what objects of this class represent and what their fields are.

    class LSystem(object):
        '''An LSystem represents ...
        fields: base, the ...
                rules, a list of ...
        '''

    Then we define the __init__ method, which is executed when a LSystem object is created. The init method should set up the fields of a class and give them reasonable initial values. Init methods often take optional arguments. You can make an argument optional by assigning a value to it in the argument list of the function declaration. For example, the following function has two arguments, the second of which is optional with a default value of 5.

    def boo( a, b = 5 ):
        print a, " ", b
    

    LSystem's init method should have two arguments: self, and an optional filename. If the function receives a filename, it should read LSystem information from the file by calling the read method of self (which you'll create below), passing the filename as the argument.

    def __init__(self, filename=None):

    The LSystem init should create two fields: base and rules. The field base should be initialized to the empty string. The field rules should be initialized to an empty list. To create a field of a class, put the prefix self. in front of the name of the field and assign something to it. The line of code below creates the field base and assigns it the empty string.

    self.base = ''

    Here is the template for the init function

        def __init__( self, filename = None ):
            '''docstring'''
            # assign to the field base, the empty string
            # assign to the field rules, an empty list
            # if the filename variable is not equal to None
                # call the read method of self with filename as the argument
    
  3. Create mutator and accessor methods for the base string: setBase(self, newbase), getBase(self). The setBase method should assign the new base string to the base field of self. The getBase method should return the base field of self.
  4. Create an accessor method getRule(self, index) that returns the specified single rule from the rules field of self. Then create the method addRule(self, newrule), which should add newrule to the rules field of self (reminder: newrule is a list with two strings in it). Look at the version 1 lsystem if you need to remember how to write the method.
  5. Create a read(self, filename) method that opens the file, reads in the L-system information, resets the base and rules fields of self, and then store the information from the file in the appropriate fields (you should use the mutators self.setBase and self.addRule to do that). You can copy and paste the function code from the version 1 lsystem.py file, but it will require some modification. For example, you don't need to create a new LSystem (self already exists) and you'll need to use the new accessor methods.

    You can use the following template

        def read( self, filename ):
            '''docstring'''
            # assign to a variable (e.g. sourceFile) the file object created with filename in read mode
            # assign to a variable (e.g. lines) the list of lines in the file
            # call the close method of the file
    
            # for each element in the lines list
                # assign to a variable (e.g. words) the loop variable split on spaces
                # if the first item in words is equal to the string 'base'
                    # call the setBase method of self with the new base string
                # else if the first item in words is equal to the string 'rule'
                    # call the addRule method of self with the new rule
    

    Once you are done with all of the above steps, download and run this test function. It should nicely print out a single rule L-system read from a file and then print out a second L-system created in the test program.

  6. In order to handle multiple rules, we need to write our own method for an L-system to apply those rules. The algorithm template is below. We scan through the string, and for each character we test if there is a rule. If a rule exists, we add the replacement to a new string, otherwise we add the character itself to the new string.
        def applyRules(self, inputString):
            '''docstring'''
            # assign to a local variable (e.g. resultString) the empty string.
            #          When finished, that variable will be the result of applying the rules.
            # for each character, char, in the input string
                # set a local boolean variable (e.g. foundReplacement) to False
                # for each rule in the rules field of self
                    # if the symbol in the rule is equal to the character
                        # add to the string you're building the replacement from the rule
                        # set foundReplacement to True
                        # break (we don't want any more rules to be activated on this same character)
                # if a replacement wasn't found
                    # add char to the string you're building
            # return the result
    
  7. Create a buildString(self, iterations) method. The body will be almost identical to the buildString function from version 1. The code outline is below.
        def buildString(self, iterations):
            '''docstrings are your best friend'''
            # assign to a local variable (e.g. string) the base field of self
            # for the number of iterations
                # assign to nextString the result of calling self's applyRules method on string
                # reassign string to the value in nextString.  (What will go wrong if you don't do this step?)
            # return nextString
    
  8. Copy the following main function and put it at the bottom of your LSystem class file. Call it like we did last week. (Note: This main function really is a function, and is not a method in any class.)

    $ python lsystem.py systemA1.txt 3 strA.txt

    Be sure to put an import sys at the top of your file.

    def main(argv):
    
        if len(argv) < 4:
            print 'Usage: lsystem.py <filename> <iterations> <output file>'
            exit()
    
        filename = argv[1]
        iterations = int(argv[2])
        outfile = argv[3]
    
        lSystem = LSystem()
    
        lSystem.read( filename )
    
        print lSystem
        print lSystem.getBase()
        print lSystem.getRule(0)
    
        resultString = lSystem.buildString( iterations )
    
        outputFile = file( outfile, 'w' )
        outputFile.write(resultString)
        outputFile.close()
    
        return
    
    if __name__ == "__main__":
        main(sys.argv)
    

    You can download and run the file on any of the following examples. Systems C through G require multiple rules.

  9. Create a new file called turtle_interpreter.py. Label it as version 2. You'll want to import the turtle package, and probably the random and sys packages as well. Begin the class definition for an TurtleInterpreter class.

    class TurtleInterpreter:

  10. Create an __init__ method with the definition below. The init should call the turtle.setup function (with width = dx, height = dy ) and then set the tracer to False (if you wish).
    def __init__(self, dx = 800, dy = 800):
        # call turtle.setup
        # set the turtle tracer to false (optional)
    
  11. Create a drawString method for the TurtleInterpreter class. Except for the header, you can copy and paste it from the version 1 turtle_interpreter.py, indenting it to be within the class. The new method definition needs included self as the first argument.

    def drawString(self, dstring, distance, angle):

  12. Copy the hold function and paste it into your class. Again, you just need to indent the function so it is part of the class block and add self as the first argument to the definition.
  13. One of the goals of building the turtle interpreter is to encapsulate all of the turtle commands. Therefore, we need to make some turtle interpreter methods that let us place and orient the turtle and set its color.

    Add the following methods to your turtle interpreter class.

    • def place(self, xpos, ypos, angle=None): - the method should pick up the pen, place the turtle at location (xpos, ypos), orient the turtle if the angle argument is not None, and then put down the pen.
    • def orient(self, angle): - the method should set the turtle's heading to the given angle.
    • def goto(self, xpos, ypos): - the method should pick up the turtle, send the turtle to (xpos, ypos) and then put the pen down.
    • def color(self, c): - the method should call turtle.color() to set the turtle's color to the given argument.
    • def width(self, w): - the method should call turtle.width() set the turtle's width to the given argument.
  14. Download the test function and run it using one of the 2-rule lsystems as an argument (e.g. systemC.txt, systemD.txt, or systemF.txt).

Assignment:

As with last week, the assignment is to bring together the lsystem and turtle 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 directly. 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.)


Tasks

  1. 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 by adding new parameters to drawString. Modify your drawString function so that it supports five additional characters in the strings.
    • '<' - the left angle bracket should append 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 tuple 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.

  2. The above simulations use a new character, L, in their drawings. Change your TurtleInterpreter to recognize this character and draw a leaf, which could be drawn as a semicircle or a circle or a line (you do not need to change the color). Go back and run classtest.py. This time your scene will contain a bunch of multi-colored fallen leaves!

  3. Create a file make_image2.py and write a function that makes a forest or garden of flowers that includes at least two different multi-rule 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 1.

  4. Make two new L-systems. They can be variations on one of the ones provided, an L-system from the ABOP book, or one you create 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 make_image3.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 2.


Extensions

Each assignment will have a set of suggested extensions. The required tasks constitute about 85% of the assignment, and if you do only the required tasks and do them well you will earn a B+. To earn a higher grade, you need to undertake one or more extensions. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. One complex extension, done well, or 2-3 simple extensions are typical.

  • Have each tree exhibit some variation by modifying aspects of how it is drawn.
  • Add leaves, berries, or color to your trees by adding symbols to the rules and cases to your turtle interpreter. For each new symbol, you will need another elif case in your drawString function.
  • 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]-X
    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.

    See if you can draw the base string and rule below the image of a tree from that L-system.

  • Get fancy with required images 1 or 2. Note that fancy means using programming structures (functions, loops, conditionals, lists, or math) to make a more interesting scene.
  • Make more L-systems.
  • Demonstrate that you can create other kinds of shapes (like squares, triangles, etc) by passing strings to the turtle interpreter's drawString function.
  • Use a Python language feature new to you (not just a new library feature or function)

Writeup and Hand-In

Before handing in your code, double check that it is well-styled:

  • All variable names and function names use either camelCase or snake_case.
  • All files and functions have docstrings.
  • Comments are added to explain complicated code blocks.
  • All variable and function names are appropriately descriptive.
  • Functions are defined before any other code is added at the top level of each file.
  • In a file with any functions defined, top level code is wrapped so that it won't execute if that file is imported by another.

Make a new wiki page for your assignment. Put the label cs151s14project8 on 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 Academics server in your private handin directory.

Colby Wiki

In general, your writeup should follow the outline below.

  • A brief summary of the task, in your own words. This should be no more than a few sentences. Give the reader context and identify the key purpose of the assignment.
  • A description of your solution to the tasks, including any images you created. (Make sure your images are appropriately sized to fit onto the wiki page.) This should be a description of the form and functionality of your final code. You may want to incorporate code snippets in your description to point out relevant features. Note any unique computational solutions you developed.
  • A description of any extensions you undertook, including images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
  • A brief description (1-3 sentences) of what you learned.
  • A list of people you worked with, including students who took the course in previous semesters, TAs, and professors. Include in this list anyone whose code you may have seen. If you didn't work with anyone, please say so.
  • Don't forget to label your writeup so that it is easy for others to find. For this lab, use cs151s14project8