Homework Five: Turtles Climbing Trees


Project Writeup

The overall objective of this homework assingment was to create a program that could read an L(Lindenmayer)-System string. In other words, the program need to be able to read a multiple level string, convert parts of the strings into appropriate data types, and then have the drawShapes function (which has been used for the past two assignments) to turn elements of the string into graphics commands for the turtle.

After copying the drawShapes function from a previous program, I created a main function that asks the user for the name of an L-System string file for reading. The files that contained the L-System strings which the program read were downloaded off Professor Maxwell's website. The readlines allows the computer to read multiple lines instead of just one, and since each of the L-System strings had six lines. The order of the lines in the string are significant for L-Systems. The first line is the base, the second line is the symbol for replacement, and the third line is the rule with which to replace the symbol. For example, say the base string is "LINDENMAYER", the symbol for replacement is "E", and the rule with which to replace the symbol is "CRAZY". One iteration of this L-System string would yield "LINDCRAZYMAYCRAZYR". The fourth line of an L-System string is the number of iterations, or number of times the replacement rule is carried out. The fifth line describes how many pixels the turtle should travel on every F (character used to denote forward movement of the turtle). The last line describes the angle of which the turtle should turn right on a "-" or left on a "+" character.

Before passing the L-System string into a for loop, I had to make sure that each piece of the string was converted into its appropriate data type. For example, we would not want the number of iterations passed into the for loop to be a string. This was accomplished by parcing the string and using the index function in order to convert the appropriate parts of the string into the appropriate data type. The base, symbol, and rule were all converted into strings. The number of interations as well as the distance with which the turtle should move forward were each changed to an integer. The angle was converted into a floating type because angles frequently require decimals. After converting parts of the L-System string into appropriate data types, a print statement tells the user in a neatly formatted response what the values of the base, symbol, etc. consist of.

The next step was to create a for loop that iterates the L-System rule replacement. For this step the string function created for python needed to be imported. Using the capabilities of the string function, I called the replace function within the for loop that takes three arguments, the base string, the symbol, and the rule, and evaluates the three arguments in the manner discussed previously (think "LINDCRAZYNMAYCRAZYR"). The value for the number of iterations was entered in the range() section of the for loop. Since the variables for distance and angle are not included in the replace() function, we have to modify the drawShapes function to take these two additional parameters. Using the program as developed thus far, I was able to process the L-String in systemA.txt, and got the following result:


The next step was to add the "[" and "]" characters to the drawShapes function. The "[" chracter stores the current postion of the turtle into a list, meaning multiple locations could be stored into one list, with the most recently visted location by the turtle being the last element in the list. The list would be stored into a variable named "memory". This was accomplished through the append, postion, and heading functions. The append function is what allows multiple locations to be continously stored into the list. The "]" removes the last position from the list and moves the turtle to the next (least) position inside the memory list. This is accomplised using the pop() function. Running a more complex L-System string from the book The Algorithmic Beauty of Plants, I was able to create the following image:


The final part of the task was to add leaves to the plant image previously created. However, because of the extremely condensed level of branches in my plant, I went back to the The Algorithmic Beauty of Plants book and picked out a plant that was not as complex in order for the leaves to show up. After saving the L-System string to a file named scene5C.txt, I modified the rule string to incorporate the character "L", which referenced the shape of a leaf(which in reality in only a very small diamond) I created in my radaley_shapes.py file from the last assignment. The diamond was filled with green (color(0, 1, 0)), and using the postion and heading functions the turtle was always returned to its original starting position so as to not interfere with the shape of the plant. I was able to create the following image:


Extensions

For an extension, I made a "colors" variable, and set it equal to a list that contained three separate lists of colors. Then I found Brittany Thomas, and she helped me use the random.choice() function to pick one of the three colors I made within the colors variable, and set and filled the leaf drawn with that color. It created this image:


The other extension that I did was I replaced the leafs with circles, and I assinged a random color to each circle. I made the random color have a green base however, by hard coding a one inside the color function:
color(random(), 1.0, random())

The image that I got was:



Back To Home Page