Laboratory: Turtle GraphicsSummary:
In this laboratory, you will experiment with turtle graphics, both
reading and writing code.
Preparation
a. Start the GIMP and MediaScript.
b. Review the list of turtle graphics procedures listed at the end
of this lab.
c. In the interactions pane, create a new
300x200 image called world and show that image.
d. In the interactions pane, create three turtles
called tommy, tanya, and tucker.
All three turtles should be placed on world.
ExercisesExercise 1: Preliminary Explorations
a. Teleport tucker to the center of your image and
determine experimentally what direction it faces.
b. Determine experimentally where tanya starts.
(All turtles face the same direction, so you can use that information in
figuring out the starting point.) Hint: You'll
probably want to use a larger brush so that it's easier to see where
it is.
c. Using tucker, draw a red rectangular box 10 pixels in
from the outside edge of world. (Just draw the lines at
the border of the box; don't try to fill in the box.)
d. Without using turtle-teleport!,
write a series of instructions to move tommy to the
center of world. The turtle should leave no
trace. Make sure to put tommy's pen back down
when you reach the center.
You may wish to compare your answer to the one in
the notes on this
exercise.
Exercise 2: A Simple Drawing
a. Consider the following instructions. What do you think they do?
b. Copy and paste those instructions into the interactions pane. (Yes,
you can paste multiple instructions into the pane.) Then hit
the Enter key to test your hypothesis.
c. What do you expect to have happen if you evaluate the same set of commands
again?
d. Check your answer experimentally. Remember: you can hit the up-arrow
key and then the Enter key to repeat the instructions.
e. What do you expect to have happen if you evaluate the same set of
instructions a third time?
f. Check your answer experimentally.
g. What do you expect to have happen if you evaluate the same set of
instructions a fourth time?
h. Check your answer experimentally.
i. In the interactions pane, set tommy's brush
to "Circle Fuzzy (15)" and the color to yellow.
j. Evaluate the set of instructions three more times.
k. In the interactions pane, set tommy's brush
to "Circle Fuzzy (11)" and the color to black.
l. Evaluate the set of instructions three more times.
m. Using similar techniques, make this drawing a bit more interesting.
Exercise 3: Varying the Drawing
a. Clear the world, move tommy back to the middle, and
reset tommy's brush by
typing the following commands in the interactions pane. (You can also
copy and paste the commands if you find that easier.)
(image-select-all! world)
(image-clear-selection! world)
(image-select-nothing! world)
(context-update-displays!)
(turtle-teleport! tommy (/ (image-width world) 2) (/ (image-height world) 2))
(turtle-face! tommy 0)
(turtle-set-brush! tommy "Circle (01)")
(turtle-set-color! tommy "black")
b. The following code adds one extra line to the previous set of instructions.
What do you expect to happen if you copy this code to your interactions
pane and then evaluate it three times?
(turtle-turn! tommy 120)
c. Check your answer experimentally.
d. What do you expect to happen if you add the following line to the
end of the previous code and evaluate it a few times?
(turtle-turn! tommy 30)
e. Check your answer experimentally. Here's the new set of instructions
for you to copy and paste:
(turtle-turn! tommy 120)
(turtle-turn! tommy 30)
f. What do you expect to happen if you add the following line to the
end of the previous code and evaluate it a few times?
(turtle-forward! tommy 10)
g. Check your answer experimentally. Here's the new set of instructions
for you to copy and paste:
(turtle-turn! tommy 120)
(turtle-turn! tommy 30)
(turtle-forward! tommy 10)
h. How might you use the techniques we just explored to generate more
complex images? Be prepared to share your answer with the class.
Exercise 4: More Figures
a. Clear the image and reset tommy, as in the previous
exercise.
b. Consider the following code.
Suppose we defined angle as 72. What do you expect the
code to produce?
c. Check your answer experimentally. That is, add a definition
for angle to the interactions pane, cut-and-paste the
code in the interactions pane, and then hit Enter.
d. What do you expect to happen if we evaluate the code a few more times?
e. Check your answer experimentally.
f. Suppose we defined angle as 144, rather than 72.
What effect would this have on the drawing?
g. Check your answer experimentally.
h. Try a few other angles and see what kinds of images you can
produce by repeatedly evaluating the expression. For
example, you might try 45, 60, 75, and 150.
For Those With Extra TimeExtra 1: Spirals
Write a series of instructions to have a turtle draw a spiral.
(It's fine if the spiral is jagged.)
Extra 2: Six-Pointed Stars
One technique for making six-pointed stars is to overlay two
equilateral triangles. Using the instructions you've already seen
for making equilateral triangles, write a series of instructions
to make a six-pointed star.
Explorations
In exercise 3, you learned that a few extra changes at the end of
a drawing can lead to an attractive sequence of drawings. In
exercise 4, you learned how to make a pentagon and a five-sided star.
Make a few changes to the code for one of those two figures (or
some other variant), similar to those we made in exercise 3, to
generate an image you find visually appealing.
Notes on the ExercisesNotes on Exercise 1: Preliminary Explorations
There are a number of ways to get tommy to the center
of the image, given that we know that tommy is at
(0,0) and facing right. Here is a fairly straightforward one.
We move tommy forward half of the width of the image,
turn right, move tommy forward half of the height of
the image, and then turn back to the original heading.
(turtle-up! tommy)
(turtle-forward! tommy (/ (image-width (turtle-world tommy)) 2))
(turtle-turn! tommy 90)
(turtle-forward! tommy (/ (image-height (turtle-world tommy)) 2))
(turtle-turn! tommy -90)
(turtle-down! tommy)
Return to the
exercise.
Reference