In this project, we made images using the turtle; we gave it commands by translating letters and symbols from a text file into things the turtle would do, such as move forward and turn. First, a text file would define a base and a rule; for each base "F", for instance, the rule (say, "F-F+F") would replace it, and the number of times this replacement would happen was indicated by the "iterations," which could be changed each time the object was drawn. We first made abstract scenes, then a grid arrangement of trees, and lastly, a scene utilizing turtle graphics objects as well as L system-derived drawings.
Task 1 was to make an abstract scene out of two or more L systems. I accomplished this by first making a list, then calling a loop to go over it, checking using if statements which file (in string) it was. Depending on the shape, the color, location, forward distance, etc. were set. For instance, the following code checked if the file was "SystemA1", then drew it with some distance, angle, and color specifications.
To move the turtle so the next object would be drawn somewhere else, I would just tell the turtle to get off the canvas (turtle.up()), give it movement commands (forward(), setx, sety, etc.), then get back on the canvas (turtle.down()). The finished image is shown below.
For task 2, we had to create a grid of trees that displayed increasing angles from left to right and increasing iterations from top to bottom. The basic idea for the grid arrangement comes from the following code:
It loops over each item in the list I provided (the column list), and for each of those, it loops over the rows I provided in a row list. It sets the x coordinate to what was provided by the column list and the y coordinate to what was provided in the row list. It then goes down and draws the tree. The maketree function works in a way that can be shown through the following code:
As you can see, the function first opens a file called "SystemB.txt" (or whatever text file it should be). Then it follows the rule and replaces every instance of the base "iterlist.pop()" times ("iterlist" contains numbers in the backwards order of what I want the iterations to be each time). The instructions said to use a double for-loop to create the grid, so even though I could have made the grid-making function contain a loop over a list of iterations (and angles) defined within the function, I had to use a different method. This popping method, however, removes the item after taking it away, which is where my replacement lists came in. The following code below demonstrates the concept:
The angle and iteration lists are there, but if I want to create a grid again after, it would be empty and nothing would happen. Therefore, I called the restock function (in an extension I'll describe later) after each time I drew a new grid arrangement. It would take the contents of one list and copy it into the original list (without losing anything); this means I had to use the .extend method for lists instead of append, since append creates a list within a list, and that would not allow the function to work.
Task 2's result is shown below.
!Screen Shot 2015-11-01 at 5.54.23 PM.png|width=300!Task 3 was to build a non-abstract scene using various L-systems. I made a snowy winter scene, which was composed of four of one kind of tree L system, two of another, 49 (some are cut off) of a snowflake-like L system, and three basic turtle rectangles of various colors. Drawing the rectangles was just a matter of making the functions to draw them, then calling them later on. For the L systems, I first defined what one individual instance of that object was expected to look like, then made a function that, like the grid-making function from before, loops over for x and y coordinates and puts a drawing of the L system object there. In the case of the plants, only one y coordinate was provided, since I wanted there to be neat row of the plants (meaning they'd have the same y coordinate). For the snowflakes, I changed the coordinate lists to have numbers that were simply placeholders for the loop to take place, and the actual place the turtle would move to would be provided later, using turtle.setx(random.randint(-300,300)) (for the x coordinate, and similarly for the y coordinate but with different integer ranges and using "sety" instead of "setx"). The image is shown below.
I did extension 6, which was to draw something additionally on each tree. In my case, I added berries, which was not very difficult. I just created a copy of the L system text file SystemB.txt, added "o" to two places on the rule. These locations were chosen because "]" represents the end of each branch, whereupon the turtle would return to the point saved before (where the branch splits). This is shown below:
At any rate, this is where the berry would be, so I added a new elif statement into the turtle interpreter file, which checks if an "o" is there; if it is, the turtle will draw a circle with the color red. After the grid function was run again, this time using the text file with "o"s added, the picture below was generated.
For extension 4, I had to give the grid function in Task 2 the parameters x, y, and scale (I also added color), meaning it could be drawn anywhere at any scale (and color). I simply added multiplied each item in the coordinate lists by the scale and added the result to the input x/y coordinates. Lastly, I made the distance the turtle moved forward by for each time the L system was drawn be multiplied by the provided scale. To show the function of this program, a picture of the result of calling the grid to be made four times at different coordinates and with different scales and colors is provided below. As I talked about before, the replacement lists take care of the problem of the lists being popped empty after creating a grid.
Through this project, I learned how to make L systems to translate characters into turtle commands. I learned how to refill popped lists using the "extend" method, and also had my understanding of looping methods reinforced; in particular, making the grid function taught me how to make rows and columns of things through the use of lists. It also worked as a refresher on the turtle commands, since it's been a while since we worked with it.