Functional Problem Solving (CSC 151 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Summary: In this laboratory, you will experiment with turtle graphics. In particular, you will both read and write code for drawing with turtle graphics.
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 interactions pane, create a new 300x200 image and show that image. Make note of the image number associated with that image.
> (image-show (image-new 300 200)) 1
d. In the definitions pane, associate the
name world with that number.
(define world 1)
d. In the definitions pane, create three turtles
called tommy, trish, 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.
a. Teleport tucker to the center of your image and
determine experimentally what direction it faces. Note that you can
use ( to teleport the turtle. Moving it
forward will give you an indication of what direction it faces. You may
have to show the image again to see the mark.
turtle-teleport!
tucker x
y)
b. Determine experimentally where trish starts.
(All turtles face the same direction, so you can use that information
in figuring out the starting point.) Hint:
You may want to use a larger brush so that it's easier to see where
it is. The brush "2. Block 03", while strangely named,
is pretty large, and has some interesting effects. (You can also use
the optional extra parameter to turtle-set-brush!
to get a larger brush.)
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 one of the mechanisms for
repeating steps, we recommend that 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.
a. Add the following procedure to your definitions pane.
;;; Procedure:
;;; turtle-center!
;;; Parameters:
;;; turtle, a turtle
;;; Purpose:
;;; Places turtle at the center of its world, facing right.
;;; Produces:
;;; turtle, the same turtle
(define turtle-center!
(lambda (turtle)
(turtle-teleport! turtle
(/ (image-width (turtle-world turtle)) 2)
(/ (image-height (turtle-world turtle)) 2))
(turtle-face! turtle 0)))
b. Determine experimentally whether that procedure successfully centers
tommy (or another turtle of your choice) in
world.
a. Copy and paste this procedure into the definitions pane.
(define figure02!
(lambda (turtle)
(turtle-forward! turtle 50)
(turtle-turn! turtle 120)
(turtle-forward! turtle 50)
(turtle-turn! turtle 120)
(turtle-forward! turtle 50)))b. If you didn't already, be sure to give an apppropriate citation to that procedure in your definitions.
c. What do you think the procedure does?
c. Check your answer experimentally.
>(turtle-center! tommy)>(figure02! tommy)>(image-show world)
d. What do you expect to have happen if run
again?
figure02!
e. Check your answer experimentally. Remember: you can use Escape-P or Ctrl-UpArrow and then the Enter key to repeat the instruction.
f. What do you expect to have happen if you run
a third time?
figure02!
g. Check your answer experimentally.
h. What do you expect to have happen if you run
a fourth time?
figure02!
i. Check your answer experimentally.
j. In the interactions pane, set tommy's color to yellow
and brush to "2. Hardness 075" with a size of 20.
k. Run three more
times, using figure02!.
repeat
>(repeat 3 figure02! tommy)
l. In the interactions pane, set tommy's color to black
and brush to "2. Hardness 075" with a size of 15.
m. Run three more
times.
figure02!
n. Using similar techniques, make this drawing a bit more interesting.
a. Add the following line to the end of the code for
.
figure02!
(turtle-turn! turtle 120)
b. Write instructions in the interactions pane to create and show
a new 300x200 image. Make note of the image number. Update the
definition of world to refer to that new image number.
Click Run to incorporate that definition and to load
the new definition of .
figure02!
c. How do you expect the new version of
to differ from the prior version?
figure02!
d. Check your answer experimentally. (Make sure to run
a few times.)
figure02!
>(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, run
a few times, and refresh the image.
figure02!
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.
Consider the following procedure.
(define figure04!
(lambda (turtle angle)
(turtle-forward! turtle 50)
(turtle-turn! turtle angle)
(turtle-forward! turtle 50)
(turtle-turn! turtle angle)
(turtle-forward! turtle 50)
(turtle-turn! turtle angle)
(turtle-forward! turtle 50)
(turtle-turn! turtle angle)
(turtle-forward! turtle 50)
))
a. Suppose we used 72 for angle. What figure do you expect one
call to to produce?
figure04!
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.
Write a series of instructions to have a turtle draw a spiral. (It's fine if the spiral is jagged.)
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.
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.
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)
(turtle-new
image)
image.
(turtle-clone
turtle)
turtle
(same position, direction, color, brush, etc.).
(turtle-forward!
turtle
distance)
turtle forward by the specified distance.
(turtle-teleport!
turtle
col
row)
turtle to
(col,row).
Do not draw along the way.
(turtle-face!
turtle
angle)
turtle face the direction specified by
angle (clockwise
from right).
(turtle-turn!
turtle
angle)
turtle clockwise by
angle degrees.
(turtle-down!
turtle)
turtle's brush down. When the turtle
moves forward, it draws with the brush.
(turtle-up!
turtle)
turtle's brush. When
turtle moves forward, it
will not draw.
(turtle-set-brush!
turtle
brush)
turtle draws with.
(turtle-set-brush!
turtle
brush
size)
turtle draws with. The brush must
be one of the resizable brushes.
(turtle-set-color!
turtle
color)
turtle draws.
(turtle-show!
turtle
angle)
turtle visible, so that it is
displayed after each call to
turtle-forward!,
turtle-turn!,
turtle-teleport!, and
turtle-face!.
Visible turtles are generally much slower.
Useful primarily for novices and for generating interesting
patterns.
(turtle-hide!
turtle
angle)
turtle invisible, so that it is
not displayed. (That is, undoes turtle-show!.)
(turtle-angle
turtle)
(turtle-col
turtle)
(turtle-point
turtle)
(turtle-row
turtle)
(turtle-world
turtle)
turtle
resides.
(repeat
i
proc!
val)
(proc!
val)i times.