Homework 5

Turtles Climbing Trees

Task:

The task of Turtles Climbing Trees was to explore and impliment L-systems. In doing so, we used python to import a text file which contained the L-system previously created in the side program stringfun.py. The python program that called this file and called turtle graphics to draw from the script was called lsystem.py. Within lsystem.py, the user is prompted to enter a filename to read from, the user then enters in a file filename.txt. The program then runs a loop parameterized by variables from the L-system in the text file. Once the loop is completed, the L-system has been changed into a longer string of characters which is then passed into the drawshape(base,distance,angle) function defined within the program. This function reads the string and follows the assigned tasks for each variable. For this program, the L-systems were taken from 'The Algorithmic Beauty of Plants.'

The program lsystem.py specifically used variables and characters in the string to represent commands. Specific variables were F for forward, which was then defined by a variable (distance) in the L-system, + for left and - for right, both defined by the variable (angle) in the L-system, and the open bracket [ to append turtle position and heading and the close bracket ] to restore the turtle's heading and position.

Storing and Restoring the Turtle State:

As mentioned previously, the turtle's position and heading can be appended to memory with the calls mem.append(position()) and mem.append(heading()). This occurred whenever the function drawshape(base,distance,angle) read an open bracket [ . In order to append to the memory, the memory must first be defined, as it was in the drawshape(base,distance,angle) function under mem=[]. This effectively set up an empty list in the variable 'mem'. Whenever mem.append(coordinate) is called, this appends the observed coordinates into the mem list in the order that they are appended. An example of such would be to start with mem=[], and call mem.append(3), then mem.append(5), and finally mem.append(1). If print mem was entered, a list [3 5 1] would be shown. To recall the appended position and heading, mem must be called again, this time with the call mem.pop(). In this application, this call gets the last-entered coordinate from mem[]. In order to restore the turtle's original position and heading, the setheading() and goto() commands must be called in reverse order since heading was saved last and will be read first. The resulting calls for mem.pop() for heading and position are setheading(mem.pop()) and goto(mem.pop()). In the context of the previously given example, the first time print mem.pop() was called, [1] would be printed, the second time print mem.pop() was called, [5] would be printed. This was a highly effective and easy method for saving the turtle's position and later restoring it to this state.

L-system for each image and the resulting image:

Image A:

Image A from lsystem.py:

Image B:

Image B from lsystem.py:

Image C:

Image C from lsystem.py:

Explanation of Extension:

As an extension, I added green leaves to a tree L-system that I picked and modified from ABOP. This is seen above in 'Image B'. I picked a slightly more complex tree that could still be recognized by the drawshape(base,distance,angle) function. I recognized that I could properly place a leaf on the end of a branch by finding the close bracket ] and inserting L just before it. I determined this since the close bracket ] is the call to restore the saved position and heading, therefore, I realized that that signified the end of a branch.