;; **************************************** ;; * Csu 211 : 1/17/2008 ;; * Lecture #5 Code ;; * Structures and Conditionals ;; * (A Drawing Program) ;; **************************************** ;; A World is (make-world img pnt draw? pen) where ;; img is a n Image, ;; pnt is a Posn, ;; draw? is a Boolean, and ;; pen is a Pen (define-struct world (img pnt draw? pen)) ;; A Pen is (make-pen color size shape) where ;; color is a String ;; size is a Number ;; shape is a Symbol (define-struct pen (color size shape)) ;; shape: String Number Symbol -> Image ;; Create a representative Image for the current Pen. (define (shape col sz sh) (cond [(symbol=? sh 'circ) (circle sz 'solid col)] [(symbol=? sh 'tri) (triangle (+ (* sz 2) 1) 'solid col)] [else (rectangle (* 2 sz) (* 2 sz) 'solid col)])) ;; image: World -> Image ;; Create a Image that represents the current World (define (image w) (overlay/xy (world-img w) (posn-x (world-pnt w)) (posn-y (world-pnt w)) (shape (pen-color (world-pen w)) (pen-size (world-pen w)) (pen-shape (world-pen w))))) ;; draw: World -> Scene ;; Draw the current World representation into an Empty Scene (define (draw w) (place-image (image w) 200 200 (empty-scene 400 400))) ;; update-pnt: Number Number -> Posn ;; Update the current Pen Point (define (update-pnt x y) (make-posn (- x 200) (- y 200))) ;; update-draw: Boolean Symbol -> Boolean ;; Update the decision to draw (update) into the current ;; World Image (define (update-draw d s) (cond [(symbol=? 'button-down s) true] [(symbol=? 'button-up s) false] [else d])) ;; update-image: World -> Image ;; Update the world image based on the draw decision (define (update-image w) (cond [(world-draw? w) (image w)] [else (world-img w)])) ;; mouse: World Number Number Symbol -> World ;; Update the World based on a Mouse Event (define (mouse w x y s) (make-world (update-image w) (update-pnt x y) (update-draw (world-draw? w) s) (world-pen w))) ;; update-color: String Char -> String ;; Change the Pen color based on a Key Press (define (update-color col c) (cond [(char=? #\r c) "red"] [(char=? #\b c) "blue"] [(char=? #\g c) "green"] [(char=? #\w c) "white"] [else col])) ;; update-size: Number Char -> Number ;; Change the Pen size based on a Key Press (define (update-size sz c) (cond [(char=? #\1 c) 1] [(char=? #\2 c) 4] [(char=? #\3 c) 8] [(char=? #\4 c) 16] [else sz])) ;; update-shape: Symbol Char -> Symbol ;; Change the Pen shape based on a Key Press (define (update-shape sh c) (cond [(char=? #\c c) 'circ] [(char=? #\t c) 'tri] [(char=? #\s c) 'square] [else sh])) ;; update-pen Pen SymbolOrChar -> Pen ;; Change the Pen based on a Key Press (define (update-pen p s/c) (cond [(char? s/c) (make-pen (update-color (pen-color p) s/c) (update-size (pen-size p) s/c) (update-shape (pen-shape p) s/c))] [else p])) ;; key: World SymbolOrChar -> World ;; Update the World based on a Key Press (define (key w s/c) (make-world (world-img w) (world-pnt w) (world-draw? w) (update-pen (world-pen w) s/c))) ;; The initial world!! (define initial-world (make-world (rectangle 400 400 'solid 'white) (make-posn -500 -500) false (make-pen "red" 9 'circ))) ;; Setup for the Drawing Window and Events (big-bang 400 400 1 initial-world) (on-mouse-event mouse) (on-key-event key) (on-redraw draw)