Due: Tuesday, April 11, 2017, 11:59 pm
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.
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 Lsystem and TurtleInterpreter classes to have certain methods with particular names.
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.
Do not forget the class docstrings and method docstings.
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 method
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
The Lsystem init method should have two arguments:
filename. If the method 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
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
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 method
def __init__( self, filename = None ): # assign to the field base, the empty string # assign to the field rules, the empty list # if the filename variable is not equal to None # call the read method of self with filename as the argument
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.
addRule(self, newrule), which should add the 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. (Note that we are not going to include getRule because we won't be needing it this week and it would actually need to be removed next week.)
read(self, filename)method that opens the file, reads in the Lsystem 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.addRuleto 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 ): # assign to a variable (e.g. fp) 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.
def replace(self, inputString): # assign to a local variable (e.g. newString) the empty string # for each character c in the input string (inputString) # set a local variable (e.g. found) to False # for each rule in the rules field of self # if the symbol in the rule is equal to the character # add to newString the replacement from the rule # set found to True # break # if not found # add to newString the character c # return newstring
buildString(self, iterations)method. It will be almost identical to the buildString function from version 1. The code outline is below.
def buildString(self, iterations): # assign to a local variable (e.g. newString) the base field of self # for the number of iterations # assign to newstring the result of calling the replace method of self # return newstring
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 iterations = int(argv) outfile = argv lsys = Lsystem() lsys.read( filename ) lstr = lsys.buildString( iterations ) fp = file( outfile, 'w' ) fp.write(lstr) fp.close() 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.
__init__method with the definition below. The init should call
turtle.setup(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)
def drawString(self, dstring, distance, angle):
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.
When you are done with the lab exercises, you may begin Project 8.
© 2017 Ying Li. Page last modified: .