Lab 8: Turtle graphics
Note: Whenever you design or write a function, you need to follow the design recipe.
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.
; 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])
(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
; 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])
50 * sin(dir * pi / 180)
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.
(define hexagon-trip (repeat 6 (list (make-draw 50) (make-turn 60))))
(define ring-trip (repeat 36 (list (make-draw 100) (make-turn 130))))
4 Challenge
(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")))