Homework Ten: Buildings and Trees


Project Writeup

Last week we had a project to add methods to a l-system class skeleton created by Professor Maxwell. This class had methods to set, generate, modify, read, and write lsystems in a proper format. This project required that we modify the l-system class to be able to handle multiple rules, while last week's project only required that the class be able to handle one rule (a list with the first item in the list being the symbol, and the second item in the list being the replacement string). After updating the l-system class, the goal was to use the ZTurtle class created in last week's lab to draw the l-system string created by the new l-system class. Due to writing the ZTurtle class that essentially converts turtle commands into appropriate actions in Zelle graphics, we can combine the plant-like objects created by processing the l-systems with the buildings created with Zelle graphics in project 6.

The major change that had to be made in the lsystem2.py file was changing the self.rules field into an empty dictionary instead of a list, and then revising the rest of the methods to be able to handle a dictionary instead of a list. Lists allow us to store and retireve items from sequential collection, and we look up items by using an index. A dictionary provides a more efficient way to look up items in a collection by using a key-value pair, a technique known as mapping. The main use for a dictionary is to look up the value associated with a particular key. Using a dictionary is very effective for handling l-systems because for an l-system string, we set keys equal to symbols and set their values equal to the appropriate replacement string.

If we were to use a list to store multiple rules, we would have to iterate through each rule list within the list of rules, and then use a conditional if statement to check and see if the first item in each rule list (the symbol) matched the character in the base string for that particular iteration before assigning the replacement string in place of that symbol. By using a dictionary, we can look up a symbol in the dictionary, instead of iterating through each rule and indexing the first item as we would have to do with a list.

My algorithim for creating my final list was first creating a bunch of l-system classes, and then setting them equal to l-systems either from the Algorithmic Beauty of Plants, or ones created in previous sections by Professor Maxwell. I appended each of these l-system objects to a list, and then used a for loop to iterate through each item in the list. Before the for loop I set two variables, shiftX and shiftY, equal to 0. With every iteration through the for loop, I added one to each of the variables. This allowed me to shift the the plants by a controlled amounted with each iteration through the list. However, the first step I took within the for loop was to create a turtle class object, so that I could place the "turtle" in different spots in the window. The key step in the for loop was assigning each l-system class to the lsystem method of the turtle class, which generates a l-system string, and then parses it into turtle commands. After this I was able to call the draw method on each of the l-systems which had been converted into turtle commands.

To draw my buildings from project 6, I imported the file with which I created the definitions for the buildings. I used for loops to call the building1() and building2() functions to draw them. I took advantage of the random package to add some randomness to where the buildings were placed. However, I made sure to add some hard coded places to each of the coordinates so that I could control their placement to the extent that I knew they would be placed in the bottom right-hand side of the window. The image below demonstrates the image created from my algorithim, which integrated the LSystems and ZTurtle classes into one application.



The l-systems that were used in my image are listed below. In the image, L1 appears farthest to the left and L5 is the tree farthest to the right.



Notice that the L3 lsystem has two rules. The image below isolates the two-rule l-system tree with seven iterations instead of six.




Back To Home Page