CSC151.01 2009F Functional Problem Solving : Labs

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

Exercises

Exercise 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: 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 canvas.

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

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

d. What do you expect to happen if we do a few more calls to figure04!?

e. Check your answer experimentally.

f. Suppose used 144, rather than 72, for the angle. 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 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 diretion 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-color! turtle color)
MediaScheme Turtle Procedure. Set the color in which turtle draws.
(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.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-9 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)

This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.