;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname 21.8.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
; Worked exercise 21.8.1
; A placed-shape is either a a placed-circ or a placed-rect
; INSERT YOUR DEFINITIONS OF placed-circ AND placed-rect HERE
(define shape-1 (make-placed-circ (make-posn 3 8) 5)) ; may vary with how you wrote placed-circ
(define shape-2 (make-placed-rect (make-posn 15 21) 12 8)) ; may vary with how you wrote placed-rect
#|
; function-on-placed-shape : a template for functions that take in a placed-shape
(define (function-on-placed-shape s)
; s a placed-shape, i.e. either a placed-circ or a placed-rect
(cond [(placed-circ? s) (function-on-placed-circ s)]
[(placed-rect? s) (function-on-placed-rect s)]
[else ...] ; error handling
)
)
; function-returning-placed-shape : a template for functions that return a placed-shape
(define (function-returning-placed-shape ...)
(cond [... (function-returning-placed-circ ...)]
[... (function-returning-placed-rect ...)]
)
)
|#
; circ-perimeter : placed-circ -> number
(define empty-circ (make-placed-circ (make-posn 0 0) 0))
(define circ-1 (make-placed-circ (make-posn 10 4) 1))
(check-within (circ-perimeter empty-circ) 0 .01)
(check-within (circ-perimeter circ-1) 6.28 .01)
(check-within (circ-perimeter shape-1) 31.4 .1)
(define (circ-perimeter c)
; Inventory and body may vary with how you wrote placed-circ.
; c placed-circ
; (placed-circ-center c) posn
; (placed-circ-radius c) number
(* pi 2 (placed-circ-radius c))
)
; rect-perimeter : placed-rect -> number
(define empty-rect (make-placed-rect (make-posn 0 0) 0 0))
(define horiz-line (make-placed-rect (make-posn -1 0) 2 0))
(define square-2 (make-placed-rect (make-posn 1 1) (sqrt 2) (sqrt 2)))
(check-expect (rect-perimeter empty-rect) 0)
(check-expect (rect-perimeter horiz-line) 4)
(check-within (rect-perimeter square-2) 5.66 .01)
(check-expect (rect-perimeter shape-2) 40)
(define (rect-perimeter r)
; Inventory and body may vary with how you wrote placed-rect.
; r placed-rect
; (placed-rect-top-left r) posn
; (placed-rect-width r) number
; (placed-rect-height r) number
(* 2 (+ (placed-rect-width r) (placed-rect-height r))))
; perimeter : placed-shape -> number
(check-within (perimeter empty-circ) 0 .01)
(check-within (perimeter empty-rect) 0 .01)
(check-within (perimeter circ-1) 6.28 .01)
(check-within (perimeter square-2) 5.66 .01)
(check-within (perimeter shape-1) 31.4 .1)
(check-within (perimeter shape-2) 40 .1)
(define (perimeter s)
; s a placed-shape, i.e. either a placed-circ or a placed-rect
(cond [(placed-circ? s) (circ-perimeter s)]
[(placed-rect? s) (rect-perimeter s)]
))
; monolithic-perimeter : placed-shape -> number
; just like perimeter, but without helper functions
(check-within (monolithic-perimeter empty-circ) 0 .01)
(check-within (monolithic-perimeter empty-rect) 0 .01)
(check-within (monolithic-perimeter circ-1) 6.28 .01)
(check-within (monolithic-perimeter square-2) 5.66 .01)
(check-within (monolithic-perimeter shape-1) 31.4 .1)
(check-within (monolithic-perimeter shape-2) 40 .1)
(define (monolithic-perimeter s)
(cond [(placed-circ? s)
; s placed-circ
; (placed-circ-center s) posn
; (placed-circ-radius s) number
(* pi 2 (placed-circ-radius s))]
[(placed-rect? s)
; s placed-rect
; (placed-rect-top-left s) posn
; (placed-rect-width s) number
; (placed-rect-height s) number
(* 2 (+ (placed-rect-width s) (placed-rect-height s)))]
))
; move-shape : placed-shape number(dx) number(dy) -> placed-shape
(check-expect (move-shape (make-placed-circ (make-posn 5 12) 4) 6 -3)
(make-placed-circ (make-posn 11 9) 4))
(check-expect (move-shape (make-placed-rect (make-posn 19 10) 8 13) -5 6)
(make-placed-rect (make-posn 14 16) 8 13))
; INSERT YOUR DEFINITION OF add-posns (exercise 20.5.4) HERE
(define (move-shape it dx dy)
; it placed-shape
; dx number
; dy number
(cond [(placed-circ? it)
; (placed-circ-center it) posn
; (placed-circ-radius it) number
(make-placed-circ (add-posns (placed-circ-center it)
(make-posn dx dy))
(placed-circ-radius it))]
[(placed-rect? it)
; (placed-rect-top-left it) posn
; (placed-rect-width it) number
; (placed-rect-height it) number
(make-placed-rect (add-posns (placed-rect-top-left it)
(make-posn dx dy))
(placed-rect-width it)
(placed-rect-height it))]
))