Skip to main content

Laboratory: Turtle Graphics

Summary: 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 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.

Exercises

Exercise 1: Preliminary Explorations

a. Teleport tucker to the center of your image and determine experimentally what direction it faces. Note that you can use (turtle-teleport! tucker x y)`{:.signature} 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.

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. Hardness 050", while strangely named, has some interesting effects. (You should also use the optional extra parameter to turtle-set-brush! to get a larger brush. Use a number like 50 or 100.)

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.

Exercise 2: Centering Turtles

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.

Exercise 3: A Simple Drawing

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?

d. Check your answer experimentally.

> (turtle-center! tommy)
> (figure02! tommy)
> (image-show world)

e. What do you expect to have happen if run figure02! again?

f. Check your answer experimentally. Remember: you can use Escape-P or Ctrl-UpArrow and then the Enter key to repeat the instruction.

g. What do you expect to have happen if you run figure02! a third time?

h. Check your answer experimentally.

i. What do you expect to have happen if you run figure02! a fourth time?

j. Check your answer experimentally.

k. In the interactions pane, set tommy’s color to yellow and brush to "2. Hardness 075" with a size of 20.

l. Run figure02! three more times, using repeat.

> (repeat 3 figure02! tommy)

m. In the interactions pane, set tommy’s color to black and brush to "2. Hardness 075" with a size of 15.

n. Run figure02! three more times.

o. 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. 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 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, run figure02! a few times, and refresh the image.

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.

(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 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 Time

Extra 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 Exercises

Notes 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

(turtle-new image)
MediaScheme Turtle Constructor. Build a new turtle that draws on image.

(turtle-clone turtle)
MediaScheme Turtle Constructor. Make a clone of turtle (same position, direction, color, brush, etc.).

(turtle-forward! turtle distance)
MediaScheme Turtle Procedure. Moves turtle forward by the specified distance.

(turtle-teleport! turtle col row)
MediaScheme Turtle Procedure. Move turtle to (col,*row). Do not draw along the way.

(turtle-face! turtle angle)
MediaScheme Turtle Procedure. Make turtle face the direction specified by angle (clockwise from right).

(turtle-turn! turtle angle)
MediaScheme Turtle Procedure. Rotate turtle clockwise by angle degrees.

(turtle-down! turtle)
MediaScheme Turtle Procedure. Put turtle’s brush down. When the turtle moves forward, it draws with the brush.

(turtle-up! turtle)
MediaScheme Turtle Procedure. Lifts turtle’s brush. When turtle moves forward, it will not draw.

(turtle-set-brush! turtle brush)
MediaScheme Turtle Procedure. Set the brush that turtle draws with.

(turtle-set-brush! turtle brush size)
MediaScheme Turtle Procedure. Set the brush (including the brush size) that turtle draws with. The brush must be one of the resizable brushes.

(turtle-set-color! turtle color)
MediaScheme Turtle Procedure. Set the color in which turtle draws.

(turtle-show! turtle)
MediaScheme Turtle Procedure. Make 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)
MediaScheme Turtle Procedure. Make turtle invisible, so that it is not displayed. (That is, undoes turtle-show!.)

(turtle-angle turtle)
MediaScheme Turtle Procedure. Get the angle that the turtle faces.

(turtle-col turtle)
MediaScheme Turtle Procedure. Get the column of the point at which the turtle resides.

(turtle-point turtle)
MediaScheme Turtle Procedure. Get the point at which the turtle resides.

(turtle-row turtle)
MediaScheme Turtle Procedure. Get the row of the point at which the turtle resides.

(turtle-world turtle)
MediaScheme Turtle Procedure. Determine the world on which turtle resides.

(repeat i proc! val)
Extended Scheme Procedure. Evaluate (proc! val)i times.