Laboratory: Turtle GraphicsSummary:
In this laboratory, you will experiment with turtle graphics. In particular,
you will both read and write code for drawing with turtle graphics.
Preparation
a. Start the GIMP and MediaScript.
b. Review the list of turtle graphics procedures and other procedures
listed at the end of this lab. There is at least one new procedure
listed there. See if you can figure it which it is.
c. In the definitions pane, create a new
300x200 image called world and show that image.
(define world (image-show (image-new 300 200)))
d. In the definitions pane, create three turtles
called tommy, tanya, and tucker.
All three turtles should be placed on world.
Here's one of the three definitions.
(define tommy (turtle-new world))
Note: Your definitions for these turtles should come
after your definition for 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.)
Rather than writing a procedure and using repeat, we
recommend you simply write out the instructions explicitly for this
problem.
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: Centering Turtles
a. Add the following procedure to your definitions pane.
b. Determine experimentally whether that procedure successfully centers
tommy (or another turtle of your choice) in
canvas.
Exercise 3: A Simple Drawing
a. Copy and paste this procedure into the definitions pane.
b. What do you think the procedure does?
c. Check your answer experimentally.
>(turtle-center! tommy)>(figure02! tommy)
d. What do you expect to have happen if run
figure02! again?
e. Check your answer experimentally. Remember: you can hit the up-arrow
key and then the Enter key to repeat the instruction.
f. What do you expect to have happen if you run
figure02! a third time?
g. Check your answer experimentally.
h. What do you expect to have happen if you run
figure02! a fourth time?
i. Check your answer experimentally.
j. In the interactions pane, set tommy's brush
to "Circle Fuzzy (15)" and the color to yellow.
k. Run figure02! three more
times, using repeat.
>(repeat 3 figure02! tommy)
l. In the interactions pane, set tommy's brush
to "Circle Fuzzy (11)" and the color to black.
m. Run figure02! three more
times.
n. Using similar techniques, make this drawing a bit more interesting.
Exercise 4: Varying the Drawing
a. Add the following line to the end of the code for
figure02!.
(turtle-turn! turtle 120)
b. Click Run to create a new image and to load the
new definition of figure02!.
c. How do you expect the new version of
figure02!
to differ from the prior version?
d. Check your answer experimentally. (Make sure to run
figure02! a few times.)
>(turtle-center! tommy)>(repeat 3 figure02! tommy)
e. What do you expect to happen if you add the following line to the
end of the new code for
figure02!?
(turtle-turn! turtle 30)
f. Check your answer experimentally. That is, click
Run, put tommy at the center of the
image, and then run figure02!
a few times.
g. What do you expect to happen if you add the following line to the
end of the new code for
figure02!?
(turtle-forward! turtle 10)
h. Check your answer experimentally.
i. How might you use the techniques we just explored to generate more
complex images? Be prepared to share your answer with the class.
Exercise 5: More Figures
Consider the following procedure.
a. Suppose we used 72 for angle. What figure do you expect one
call to figure04! to produce?
b. Check your answer experimentally.
c. What do you expect to happen if we do a few more calls to
figure04!?
d. Check your answer experimentally.
e. Suppose used 144, rather than 72, for the angle.
What effect would this have on the drawing?
f. Check your answer experimentally.
g. 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 4, you learned that a few extra changes at the end of
a drawing can lead to an attractive sequence of drawings. In
exercise 5, 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