CS151 HW#5
Tara Davidson '10
Write up:
-
A brief description of the task (in your own words): In this assignment
I created a function that can read an L-system from a file in my directory that
consists of a base string, a symbol for replacement, rule with which to replace
the symbol, the number of times to execute the replacement, the distance to
travel for each F symbol, and the angle to turn with each '+' or '-' symbol. This
file has all these components on separate lines. To do this, I called drawShapes
in my function but I changed drawShapes so that the parameters were now
string, distance, and angle. Now my drawShapes function could take a string of
shapes and draw them with a given distance and angle that came from a file in
my directory. Another addition to my drawShapes function was that now the function
could take the characters 'F', '+', and '-' and the turtle would go
forward(distance), turn left(angle), and turn right(angle) with those respective
characters. The first file I used was systemA.txt, which I downloaded from
the CS151 homework site. When I ran my function and entered in systemA.txt for
my file, an image was drawn according to the L-system rules from systemA. The
image was the same image that the homework site showed. I then changed parts of
systemA.txt so that a new image would be drawn. I saved my L-system as systemK.txt
and the image was scene5A (shown below). Then I added '[' and ']' to my drawShapes
function so that '[' stores the current turtle heading and position and ']'
restores the turtle's heading and position. Then I was able to run systemB.
I modified systemB to systemD which produced scene5B (shown below). Finally
I made one more addition to my drawShapes function. I created a new shape, a
very simple leaf, that could be called by the character 'L'. I put 'L' at the
beginning of the rule with which to replace the symbol in system B and created
scene5C (shown below).
-
Explain how you store and restore the turtle state At the very beginning
of my drawShapes funciton I added a variable, memory, and initialized it to an
empty list, []. I created a '[' character in my drawShapes function with the purpose
to store the current turtle heading and position. By telling the function to
append the turtle heading and append the turtle position, this information was
added to the list memory. I had to append the list once for heading and again for
position with these statements: memory.append(heading()), memory.append(position()).
Then I created a ']' character in my drawShapes function with the purpose to
restore the turtle heading and position. I was able to do this by telling the
function to lift the turtle up, go to the position stored in the memory list,
set the heading to the heading in the memory list, and then putting the turtle
down.
-
Show the L-system for each of your images
The L-system for scene5A:
-FF
F
Ft+Ft-Ft-Ft+Ft
1
10
90
Picture of scene5A:
The L-system for scene5B:
FF
F
+FF-[-F+F+F]+[+F-F-F]
3
5
22.5
Picture of scene5B:
The L-system for scene5C:
F
F
LFF-[-F+F+F+F]+[+F-F-F]
3
5
22.5
Picture of scene5C:
-
A description of and pictures for any extensions you did I incorporated
Extension 3 in scene5A by using my triangle shape and using the color green. I
also did Extension 2 and created another L-system to make scene5D (shown below):
The L-system for scene5D:
FFF
F
+FF-[-F+F+F]+[+F-F-F]
5
2
40
Picture of scene5D: