CS151 HW#10
Tara Davidson '10
Write up:
-
A brief description of the task (in your own words): This project modified
the project from last week. I continued to work with my lsystem class that I used
last week, but added the ability of my class to use a dictionary (self.rules) so
that my L-system could generate a scene that created trees with multiple rules. This
project also required using the zturtle class we created in lab last week so that
my scene could include trees as well as my buildings from project 6. To begin this
project I modified several methods in lsystem3.py. The first method I altered was
the set method. Instead of self.rules = rules, I set self.rules to an empty dictionary,
{}, and for each rule in my list of rules I assigned rule[1] to a key, rule[0].
I then modified the toString method so that the method would properly print out
the rules. I did this by just replacing the % signs from the existing rule line:
% (i, key, self.rules[key]). This prints out the rule number, the key, and the
respective rule. I then put that line within a for loop for every key in the
dictionary. I then modified the generateString method so that it could handle
multiple rules. I first made a copy of the base string. Then for every iteration I created
a variable, newbase, set to an empty string. Within the same for loop, for every
character in my base string I checked to see if my dictionary had a key like the
character in the base string. If it did, I added the replacement to newbase. If not,
I just added that character to newbase. Base then became newbase and I returned
base. Finally I editted the write and read functions so that the functions could
either read or write multiple rules. The last part of the project was creating
newprogram2.py which generates a scene with 3 trees and 4 buildings (see picture
below). In newprogram2.py I used zturtle2.py from lab so that both trees and buildings
could be in my scene and the original turtle was not needed. I also used a for
loop to draw all of my trees and save lines of code.
-
Explain how a dictionary is different from a list, and why it is appropriate
for the task of identifying the proper rule A dictionary can assign values
to characters instead of to an ordered list. This is especially helpful for this
project because we can make the key of the dictionary the symbol to be replaced
and the value the replacement string.
-
Describe your algorithm for creating the final scene 1. I created
a window. 2. I assigned the variable turtle to the set l-system, ZTurtle.
3. I created my trees by assigning variables (treeOne, treeTwo, treeThree) to
my lsystem strings. 4. I created my buildings using the same method I used in
project 6. 5. I put the turtle down. 6. I oriented the turtle up. 7. I drew
building 1. 8. I made building 2 by cloning building 1 and moving the building 2
off of building 1. 9. I made building 3. 10. I made building 4 by cloning building 3 and moving the building 4
off of building 3. 11. I made a for loop to draw all of my trees by creating a
list, listoftrees, that contained treeOne, treeTwo, treeThree, and then iterated
through the list so that each tree was moved in a different starting point and
drawn. 12. Moved the turtle out of the way. 13. I wait for the user to click
'RETURN' before closing the window.
-
Show the generated scene from newprogram2.py
-
A description of any extensions you did Extension 5: I made
multiple rules for my trees by combining rules from ABOP.