;; **************************************** ;; * Csu 211 : 1/23/2008 ;; * Lecture #6 Code ;; * Unions ;; **************************************** ;; b->string: Boolean -> String ;; Convert a Boolean into a String, "true" or "false" (define (b->string b) (cond [(and (boolean? b) b) "true"] [(boolean? b) "false"] [else (error 'none "Expected a Boolean")])) ;; Tests "b->string Tests" (string=? "true" (b->string true)) (string=? "false" (b->string false)) ;(b->string 15) ;; ==> Error... can't test this every time ;; * * * * * * * * * ;; Shape Definitions... ;; Shape is one of ;; (make-circ rad) ;; (make-square w) ;; (make-triang w h) ;; a Circle is: (make-circ rad) (define-struct circ (r)) ;; a Square is: (make-square w) (define-struct square (w)) ;; a Triangle is: (make-triang w h) (define-struct triang (w h)) ;; * * * * * * * * * ;; Function Template for Shapes... ;; f: Shape ... -> ??? ;; Do somthing with a Shape ;(define (f sh) ; (cond ; [(circ? sh) ... (circ-r sh) ...] ; [(square? sh) ... (square-w sh) ...] ; [(triang? sh) ... (triang-w sh) ... ; ... (triang-h sh) ...] ; [else (error 'area "Expected a Shape")])) ;; * * * * * * * * * ;; Some Useful Shape Functions ;; area: Shape -> Number ;; Return the Area of the given Shape (define (area sh) (cond [(circ? sh) (* 3.14 (sqr (circ-r sh)))] [(square? sh) (sqr (square-w sh))] [(triang? sh) (* .5 (triang-w sh) (triang-h sh))] [else (error 'area "Expected a Shape")])) ;; Tests "area Tests" (= (area (make-circ 10)) 314) (= (area (make-square 10)) 100) (= (area (make-triang 10 5)) 25) ;; perimeter: Shape -> ??? ;; Compute the Perimeter of the given Shape (define (perimeter sh) (cond [(circ? sh) (* 2 3.14 (circ-r sh))] [(square? sh) (* 4 (square-w sh))] [(triang? sh) (sqrt (+ (sqr (* .5 (triang-w sh))) (sqr (triang-h sh))))] [else (error 'area "Expected a Shape")])) ;; Tests "perimeter Tests" (= (perimeter (make-circ 10)) 62.8) (= (perimeter (make-square 10)) 40) (= (floor (perimeter (make-triang 10 5))) 7) ;; scale: Shape Number -> Shape ;; Compute a new Shape that is scaled by the given amount (define (scale sh c) (cond [(circ? sh) (make-circ (* c (circ-r sh)))] [(square? sh) (make-square (* c (square-w sh)))] [(triang? sh) (make-triang (* c (triang-w sh)) (* c (triang-h sh)))] [else (error 'area "Expected a Shape")])) ;; Tests "scale Tests" (equal? (scale (make-circ 10) 2) (make-circ 20)) (equal? (scale (make-square 10) 3) (make-square 30)) (equal? (scale (make-triang 10 5) 1.5) (make-triang 15 7.5)) ;; draw: Shape -> Image ;; Compute an image of the given Shape (define (draw sh) (cond [(circ? sh) (circle (circ-r sh) "solid" "yellow")] [(square? sh) (rectangle (square-w sh) (square-w sh) "solid" "orange")] [(triang? sh) (triangle (triang-w sh) "outline" "blue")] [else (error 'area "Expected a Shape")])) ;; Tests (checked visually) "draw Tests" (draw (make-circ 10)) (draw (make-square 10)) (draw (make-triang 10 15)) ;; * * * * * * * * * ;; Combine all the functions together ;; text-for: String Number -> Image ;; Create a Text Image for the given String+Number (define (text-for s n) (text (string-append s (number->string (/ (floor (* 10 n)) 10))) 14 'black)) ;; World is a Shape! ;; image: Shape -> Scene ;; Place the given Shape into the middle of an ;; empty Scene. Also add tests of the functions ;; above with text Images of ARea and Perim... (define (image w) (place-image (overlay/xy (overlay/xy (overlay/xy (rectangle 400 400 'outline 'gray) -200 -200 (text-for " Area: " (area w))) -200 -180 (text-for " Perim: " (perimeter w))) 0 0 (draw w)) 200 200 (empty-scene 400 400))) ;(image (make-circ 100)) ;(image (make-square 100)) ;; key: Shape SymOrChar -> Shape ;; React to a Keypress... do something to the given Shape (define (key w SoC) (cond [(char? SoC) (cond ;; Reset the Shape [(char=? SoC #\c) (make-circ 100)] [(char=? SoC #\s) (make-square 50)] [(char=? SoC #\t) (make-triang 100 100)] ;; Scale the shape up or down; On my laptop ;; these are the +/= and -/_ keys [(char=? SoC #\-) (scale w .90)] [(char=? SoC #\=) (scale w 1.1)] ;; Nothing [else w])] [else w])) ;; Setup for the Window and Events (big-bang 400 400 1 (make-circ 100)) (on-redraw image) (on-key-event key)