;; **************************************** ;; * Csu 211 : 1/28/2008 ;; * Lecture #8 Code ;; * Structure Review ;; **************************************** ;;**** NOTE: This code is broken!! It's your task ;; to fix it :( ;; Read through the whole thing... it will explain ;; lots of things I didn't have time to go over ;; in class. I removed all the Magic, and renamed ;; to more concrete things. ;; Radius Constant (define R 50) ;; meaning Where Direction 'vector' ;; A World is: (make-world Pnt Pnt) (define-struct world (p d)) ;; A Pnt is (make-pnt Number Number) ;; Represents a pair of coordinates *OR* a vector direction (define-struct pnt (x y)) ;; image: World -> Scene ;; Draw a Ball into an empty Scene (define (image w) (place-image (circle R "solid" "red") (pnt-x (world-p w)) (pnt-y (world-p w)) (empty-scene 400 400))) ;; xy-length: Number Number -> Number ;; The length of this x/y pair interpreted as a Vector (define (xy-length x y) (sqrt (+ (sqr x) (sqr y)))) ;; vec-length: Pnt -> Number ;; The length of this Pnt interpreted as a Vector (define (vec-length p) (xy-length (pnt-x p) (pnt-y p))) ;; vec-scale: Pnt -> Pnt ;; Scale this Pnt pair interpreted as a Vector, to have ;; unit length (i.e., 1) (define (vec-scale p) (make-pnt (/ (pnt-x p) (vec-length p)) (/ (pnt-y p) (vec-length p)))) ;; ** Tests ;; Here's some tests that should explain what all these ;; functions do... "Vector Tests" (= 50 (xy-length 40 30)) (= 100 (xy-length 60 80)) (= 1 (vec-length (vec-scale (make-pnt 19 -21)))) (= 1 (vec-length (vec-scale (make-pnt -14 23)))) ;; slow-speed: World -> World ;; Reduce the speed but keep the direction of the Ball ;; in the World (define (slow-speed w) (make-world (vec-scale (world-p w)) (world-d w))) ;; tick: World -> World ;; On the tick, move the ball in the right direction (define (tick w) (make-world (make-pnt (+ (pnt-x (world-p w)) (pnt-x (world-d w))) (+ (pnt-y (world-p w)) (pnt-y (world-d w)))) (world-d w))) ;; mouse: World Number Number Symbol -> World ;; Redirect the Ball on a mouse-press (define (mouse w x y s) (cond [(symbol=? 'button-down s) (slow-speed (make-world (world-p w) (make-pnt (- x (pnt-x (world-p w))) (- y (pnt-y (world-p w))))))] [else w])) ;; Setup for the window and drawing of the World (big-bang 400 400 .2 (make-world (make-pnt 200 200) (make-pnt 0 0))) (on-redraw image) (on-tick-event tick) (on-mouse-event mouse)