Fundamentals of Computer Science I: Media Computing (CS151.01 2008S)

Laboratory: Turtle Graphics


Summary: In this laboratory, you will experiment with turtle graphics, both reading and writing code.

Preparation

a. In the interactions pane, create a new 300x200 image called world and show that image.

b. In the interactions pane, create three turtles called tommy, tanya, and tucker. All three turtles should be placed on 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 she 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.

Exercise 2: A Simple Drawing

a. Consider the following instructions. What do you think they do?

(turtle-forward! tommy 50)
(turtle-turn! tommy 120)
(turtle-forward! tommy 50)
(turtle-turn! tommy 120)
(turtle-forward! tommy 50)

b. Copy and paste those instructions into the definitions pane and click Run to test your hypothesis.

c. What do you expect to have happen if you click Run again?

d. Check your answer experimentally.

e. What do you expect to have happen if you click Run a third time?

f. Check your answer experimentally.

g. What do you expect to have happen if you click Run 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. Click Run three more times.

k. In the interactions pane, set tommy's brush to "Circle Fuzzy (11)" and the color to black.

l. Click Run 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.

> (image-select-all! world)
> (image-clear-selection! world)
> (image-select-nothing! world)
> (context-update-displays!)
> (turtle-teleport! tommy 150 100)
> (turtle-face! tommy 0)
> (turtle-set-brush! tommy "Circle (01)")
> (turtle-set-color! tommy "black")

b. What do you expect to have happen if you add the following line to the end of the previous code (in the definitions pane) and then click Run 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 (in the definitions pane) and then click Run a few times?

(turtle-turn! tommy 30)

e. Check your answer experimentally.

f. What do you expect to happen if you add the following line to the end of the previous code (in the definitions pane) and then click Run a few times?

(turtle-forward! tommy 10)

g. Check your answer experimentally.

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. Also, delete all the commands in the definitions pane.

b. Consider the following code.

(turtle-forward! tommy 50)
(turtle-turn! tommy angle)
(turtle-forward! tommy 50)
(turtle-turn! tommy angle)
(turtle-forward! tommy 50)
(turtle-turn! tommy angle)
(turtle-forward! tommy 50)
(turtle-turn! tommy angle)
(turtle-forward! tommy 50)

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 definitions pane, cut-and-paste the code after that definition in the definitions pane, and then click Run.

d. What do you expect to happen if we click Run 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 hitting Run. 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 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.

Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Janet Davis, Matthew Kluber, and Samuel A. Rebelsky. (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.