Joe's CS151 Project 8 WriteUp
Skip to end of metadata
Go to start of metadata



In the 8th project for CS151, students were asked to improve upon the Lsystems from the previous project, and experiment with multiple rules. By writing text files, an Lsytem class, and altering our TurtleInterpreter, this was feasible, and allowed us to create better pictures.



The first task was to add an additional list and additional commands to the <TurtleInterpreter> class that we created in lab. This needed to be done to reduce our reliance on turtle commands. Everything can be done without importing the turtle package.

First, before I told python which characters should do certain things, I created a new list called <colorstack>. In the same way that positions and headings are appended and popped off a list, <colorstack> stores color data.

Next, I added additional Elif statements within the main For Loop of the TurtleInterpreter class to tell Python what to do when specific characters are passed to it. As per the lab’s instructions, I enabled my interpreter to set the color of turtle to red, green, and yellow, as well as save and restore the turtle’s color.

Here is a snippet of this code:

The second task asked students to create an arrangement of flowers or trees. Two of these Lsystems needed to have multiple rules, which meant that the programmer had to create text files to read the rules from. Once I remembered how my interpreter read in files and built the strings, this was not difficult. I simply drew a basic design on a sheet of paper to use as the base, and then added what seemed to be an interesting rule. In a text file, I included one line that began with “rule”, and others that began with “rule”. Following “base”, I included a space and the base to draw the initial shape. Following “rule”, I included a space, the character to be replaced, another space, and then the replacement string. Here is an image of one of the text files I read in:


By using the <read> and <buildString> methods from the Lsystem class, I replace and draw the strings. I defined calls to the methods as functions, and have included a snippet of this basic code below.


            The biggest challenge of this task was to make sure that I was saving and restoring color correctly, for when multiple rules are being used, there are often several colors to juggle.

            To create the final arrangement, I added additional characters to my interpreter to move the turtle to specific locations. For instance, the string ‘2’ moves the turtle to the location (-300,0) without drawing any lines, and the string ‘w’ moves the turtle to random locations between x=-300 and x=300 at y=-375. By including these “movement characters” in my base string but not replacing them, I easily moved my shapes around the screen into an arrangement I found pleasing. Here is an image of the final product.


As you can see, I used the string ‘w’ to place a row of flowers below the three larger images. This makes the arrangement look nicer, almost like a park.

The final task of this project was to create two more Lsystems and display them at different iterations. This gives the effect of the plant growing. I created my Lsystems in the same way, but ran into trouble with such high numbers of iterations. My trees weren’t small enough to fit on the page, and I wanted a simple way to locate the different Lsystems without writing an Elif statement for each location. Therefore, I added parameters <x> and <y> to my <drawstring> method and set their default values to zero. If the string ‘1’ is passed to the interpreter, it goes to the location specified when the method is called. Here is an image of the method getting called at a specific location


As evident, I included the x and y parameters in the functions for each Lsystem as well-- this allows multiple images to be drawn from one function. Also, I added a <distance> parameter to most of the functions so as to allow the size to be adjusted quickly. Finally, I added an <iter> variable to let my functions take in a number of iterations to run the <buildString> method.

Here is my <scene1> function, which calls an Lsystem three times with the iterations, length of lines, and location being controlled by parameters.

And here is the image of the final image I created showing the growth of two plants.




For extensions on this project, I completed three additional tasks. First, I used my knowledge of Lsystems to create shapes. I made a square, a triangle, and a string that makes an isometric grid. I wrote the code to create these shapes in the same way I created my required Lsystems, but had to think differently about what my commands were doing. The ending orientation of the turtle, in particular, was important to pay attention to, for this affected how the image would change with more iterations. Here are the images I created with simple Lsystem shapes:


The next extension was to add additional shapes and versatility to my TurtleInterpreter class, so that a single keystroke draws an entire shape or changes some aspect of the image. This was fairly simple, and merely required more Elif statements to be added to the <drawString> method. Here is an example of that code, and the various applications of this code can be seen in my images and Text Wrangler files.


Finally, I enabled my Lsystems to become more versatile with the ability to place them around the screen and change the length of each line. Thus, my Lsystems become more modular, for the size and location can be altered easily. By adding a parameter to each Lsystem function called <distance>, and placing it in the call for the <buildString> method, I allow the user to call the function many times with different lengths. Also, by adding parameters and an additional Elif statement to the <drawString> method of the TurtleInterpreter class, I allow the method to be called with x and y values and move the drawing to a specific location. Here is an image of the Elif Statement I wrote:




In this prject, I gained a more practical knowledge of how to read in and work with files, as well as how classes can be used to make programming easier. This, in turn, helped reinforce the need to be careful with indentation and syntax, for small errors can be hard to find and detrimental to the programmer’s desires.


In this project, I received help from Mathias Kallick, the TA on Sunday nights.