;; --- CSU213 Fall 2006 Lecture Notes --------- ;; Copyright 2006 Viera K. Proulx ;; Lecture 6: September 18, 2006 ;; Designing Methods for Unions of Classes ;; An IShape is one of ;; -- (make-circle Posn Number String) ;; -- (make-square Posn Number String) (define-struct circle (center radius color)) (define-struct square (nw size color)) ;; Examples: (define pt1 (make-posn 0 0)) (define pt2 (make-posn 3 4)) (define pt3 (make-posn 7 1)) (define c1 (make-circle (make-posn 50 50) 10 "red")) (define c2 (make-circle (make-posn 50 50) 30 "red")) (define c3 (make-circle (make-posn 30 100) 30 "blue")) (define s1 (make-square (make-posn 50 50) 30 "red")) (define s2 (make-square (make-posn 50 50) 50 "red")) (define s3 (make-square (make-posn 20 40) 10 "green")) ;; Template for the method that consumes Shape: ;; (define (method-name this-shape arg2) ;; (cond ;; [(circle? this-shape) ... body for circle ...] ;; [(square? this-shape) ... body for square ...])) ;; Function definitions ;; to compute the area of this shape (define (area this-shape) (cond [(circle? this-shape) (* pi (circle-radius this-shape) (circle-radius this-shape))] [(square? this-shape) (* (square-size this-shape) (square-size this-shape))])) ;; Tests: (< (abs (- (area c1) 314.1596)) 0.01) (= (area s3) 100) ;; to compute the distance form this shape to the origin (define (distTo0 this-shape) (cond [(posn? this-shape) (sqrt (+ (sqr (posn-x this-shape)) (sqr (posn-y this-shape))))] [(circle? this-shape) (- (distTo0 (circle-center this-shape)) (circle-radius this-shape))] [(square? this-shape) (distTo0 (square-nw this-shape))])) ;; Tests: (< (abs (- (distTo0 c1) 60.711)) 0.01) (< (abs (- (distTo0 s3) 44.721)) 0.01) ;; to increase the size of this shape by the given increment (define (grow this-shape inc) (cond [(circle? this-shape) (make-circle (circle-center this-shape) (+ (circle-radius this-shape) inc) (circle-color this-shape))] [(square? this-shape) (make-square (square-nw this-shape) (+ (square-size this-shape) inc) (square-color this-shape))])) ;; Tests: (equal? (grow c1 20) c2) (equal? (grow s1 20) s2) ;; is the area of this shape is bigger than the area of the that shape? (define (biggerThan? this-shape that-shape) (>= (area this-shape) (area that-shape))) ;; Tests: (boolean=? (biggerThan? c1 c2) false) (boolean=? (biggerThan? c2 c1) true) (boolean=? (biggerThan? c1 s1) false) (boolean=? (biggerThan? c1 s3) true) (boolean=? (biggerThan? s1 s2) false) (boolean=? (biggerThan? s2 s1) true) (boolean=? (biggerThan? s1 c1) true) (boolean=? (biggerThan? s3 c1) false) ;; compute the distance from this point to that point (define (distTo this-posn that-posn) (sqrt (+ (sqr (- (posn-x this-posn) (posn-x that-posn))) (sqr (- (posn-y this-posn) (posn-y that-posn)))))) ;; Tests: (<= (abs (- (distTo pt1 pt2) 5)) 0.001) (<= (abs (- (distTo pt2 pt3) 5)) 0.001) ;; does this shape (including the boundary) contain the given point? (define (contains? this-shape pt) (cond [(circle? this-shape) (<= (distTo (circle-center this-shape) pt) (circle-radius this-shape))] [(square? this-shape) (and (<= (posn-x (square-nw this-shape)) (posn-x pt) (+ (posn-x (square-nw this-shape)) (square-size this-shape))) (<= (posn-y (square-nw this-shape)) (posn-y pt) (+ (posn-y (square-nw this-shape)) (square-size this-shape))))])) ;; Tests: (boolean=? (contains? c1 (make-posn 100 100)) false) (boolean=? (contains? c2 (make-posn 40 60)) true) (boolean=? (contains? s1 (make-posn 100 100)) false) (boolean=? (contains? s2 (make-posn 55 60)) true)