;; **************************************** ;; * Csu 211 : 1/16/2008 ;; * Lecture #4 Code ;; * Booleans and Conditional Expressions ;; **************************************** ;; The 'empty-scene', only need to create once (define EMPTY (empty-scene 400 400)) ;; Create the Window to display our world (big-bang 400 400 1 false) ;; dist: Number Number -> Number ;; Compute the Distance from (0,0) to (x,y) (define (dist x y) (sqrt (+ (sqr x) (sqr y)))) "Distance Tests" (= (dist 3 4) 5) (= (dist 0 0) 0) (= (dist 9 12) 15) (= (dist -3 4) 5) ;; Definitions for our Circle (define R 150) (define CCX 200) (define CCY 200) ;; test-circ: Number Number Number Number -> Boolean ;; Is the given point (x,y) within our circle ;; centered at (cx,cy)? (define (test-circ x y cx cy) (> R (dist (- cx x) (- cy y)))) ;; image-circ: Boolean -> Scene ;; Draw our circle in an empty scene, based on ;; whether or not the mouse is 'in' the circle (define (image-circ in) (place-image (circle R (cond [in "solid"] [else "outline"]) "purple") CCX CCY EMPTY)) ;; mouse-circ: Boolean Number Number Symbol -> Boolean ;; Use a Mouse event to update our World (define (mouse-circ in x y sym) (test-circ x y CCX CCY)) ;;** Setup after the big-bang ;(on-redraw image-circ) ;(on-mouse-event mouse-circ) ;; Definitions for our Rectangle (define WIDTH 250) (define HEIGHT 200) (define RCX 200) (define RCY 200) ;; image-rect: Boolean -> Scene ;; Draw our rectangle in an empty scene, based on ;; whether or not the mouse is 'in' the rectangle (define (image-rect in) (place-image (rectangle WIDTH HEIGHT "solid" (cond (in "red") (else "blue"))) RCX RCY EMPTY)) ;; test-rect: Number Number Number Number -> Boolean ;; Is the given point (x,y) within our rectangle ;; centered at (cx,cy)? (define (test-rect x y cx cy) (and (and (> x (- cx (/ WIDTH 2))) (< x (+ cx (/ WIDTH 2)))) (and (> y (- cy (/ HEIGHT 2))) (< y (+ cy (/ HEIGHT 2)))))) ;; mouse-rect: Boolean Number Number Symbol -> Boolean ;; Use a Mouse event to update our World (define (mouse-rect in x y sym) (test-rect x y RCX RCY)) ;;** Setup after the big-bang (on-redraw image-rect) (on-mouse-event mouse-rect)