On this page:
1 Measuring trips
2 Drawing trips
3 Repeating trips
4 Challenge
8.14

Lab 8: Turtle graphics🔗

Note: Whenever you design or write a function, you need to follow the design recipe.

In this lab (and some future assignments), you’ll draw pictures by telling a turtle carrying a pen to move and turn. For example, to draw a square, the turtle can follow these instructions:
  • Move forward by a distance of 50.

  • Turn left 90 degrees.

  • Move forward by a distance of 50.

  • Turn left 90 degrees.

  • Move forward by a distance of 50.

  • Turn left 90 degrees.

  • Move forward by a distance of 50.

This way of drawing pictures is called turtle graphics.

Start with these data and structure definitions:
; A Trip is one of:
; - empty
; - (cons Step Trip)
 
; A Step is one of:
; - (make-draw Number)
; - (make-turn Number)
; *Interpretation*: angle is how many degrees to turn left (counterclockwise)
(define-struct draw [distance])
(define-struct turn [angle])

Below are two example trips. The first example is the instructions above for drawing a square. The second example makes a shape like the letter Z.
(define square-trip
  (list (make-draw 50)
        (make-turn 90)
        (make-draw 50)
        (make-turn 90)
        (make-draw 50)
        (make-turn 90)
        (make-draw 50)))
(define z-trip
  (list (make-draw 80)
        (make-turn -135)
        (make-draw 120)
        (make-turn 135)
        (make-draw 80)))

1 Measuring trips🔗

Exercise 1. Design a function step-length that computes the length of a Step. In other words, this function should compute how much ink is used by the given Step. Turning doesn’t draw anything, so the length of a turn is 0.

Exercise 2. Design a function trip-length that computes the length of a Trip. In other words, this function should compute how much ink is used by the given Trip. That is the total amount of ink used by all the Steps in the Trip. Hint: Follow the template for processing a Trip, so use step-length and trip-length.

2 Drawing trips🔗

The goal of the next 3 exercises is to draw a Trip on an Image. The following data definition and structure definition let us keep track of where the turtle is and which way it is facing:
; A Turtle is (make-turtle Number Number Number)
; *Interpretation*: dir=0 faces east,
;                   dir=90 faces north,
;                   dir=180 faces west,
;                   dir=270 faces south
(define-struct turtle [x y dir])

Exercise 3. Design a function move that takes a Step and a Turtle before taking the step and computes the Turtle after taking the step. For example, if the turtle is currently facing north, then taking the step (make-turn 45) should turn the turtle to face northwest, whereas taking the step (make-turn -90) should turn the turtle to face east. To take another example, if the turtle is currently facing north, then taking the step (make-draw 50) should decrease the y coordinate by 50. More generally, if the turtle is currently facing dir, then taking the step (make-draw 50) should decrease the y coordinate by

50 * sin(dir * pi / 180)

and increase the x coordinate by

50 * cos(dir * pi / 180)

Exercise 4. Design a function draw-step that draws a given Step taken by a given Turtle on a given Image. Use the function scene+line provided by the 2htdp/image library in your examples and your definition; choose your favorite color. Also use the function move you just designed. Recall that turning doesn’t draw anything.

Exercise 5. Now design a function draw-trip that draws a given Trip taken by a given Turtle on a given Image. Use the functions move and draw-step you just designed.

Try draw-trip on example trips such as square-trip and z-trip.

3 Repeating trips🔗

Exercise 6. Let’s make some pretty pictures. Design a function repeat that takes a NaturalNumber and a Trip and returns a new Trip that repeats the given Trip the given number of times. Hint: Follow the template for processing a NaturalNumber, and use append. This function actually works not just for trips but also for other lists.

Now this trip should be a hexagon:
(define hexagon-trip
  (repeat 6 (list (make-draw 50) (make-turn 60))))
And this trip should look like a ring:
(define ring-trip
  (repeat 36 (list (make-draw 100) (make-turn 130))))

4 Challenge🔗

Exercise 7. Some restaurants offer meal deals like this: the customer can choose one appetizer out of a list of appetizers, one entrée out of a list of entrées, and one dessert out of a list of desserts. That would make a 3-course meal, but some restaurants offer 2-course meals or 5-course meals. Design a function deals that takes a list of courses as input and produces a list of all possible meals. A course is a list of strings, and a meal is also a list of strings (exactly as many as there are courses). Here is an example of the desired behavior:
(check-expect (deals (list (list "soup" "salad")
                           (list "sandwich" "stir fry" "confit")
                           (list "chocolate" "tofu" "affogato")))
              (list (list "soup"  "sandwich" "chocolate")
                    (list "soup"  "sandwich" "tofu")
                    (list "soup"  "sandwich" "affogato")
                    (list "soup"  "stir fry" "chocolate")
                    (list "soup"  "stir fry" "tofu")
                    (list "soup"  "stir fry" "affogato")
                    (list "soup"  "confit"   "chocolate")
                    (list "soup"  "confit"   "tofu")
                    (list "soup"  "confit"   "affogato")
                    (list "salad" "sandwich" "chocolate")
                    (list "salad" "sandwich" "tofu")
                    (list "salad" "sandwich" "affogato")
                    (list "salad" "stir fry" "chocolate")
                    (list "salad" "stir fry" "tofu")
                    (list "salad" "stir fry" "affogato")
                    (list "salad" "confit"   "chocolate")
                    (list "salad" "confit"   "tofu")
                    (list "salad" "confit"   "affogato")))