;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname overlap) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;;; DO NOT paste the above lines into a blank DrRacket beginner-student window. ;;; Either download the file and open from within DrRacket, ;;; or remove the `#reader` line (and the comment) from the above as you paste into DrRacket. ; <=< : real?, real?, real? -> boolean? ; Is x in the half-open interval [a,b)? ; pre-conditon: (<= a b) ; (define (<=< a x b) (and (<= a x) (< x b))) (check-expect (<=< 4 7 10) #true) (check-expect (<=< 7 7 10) #true) (check-expect (<=< 4 7 7) #false) (check-expect (<=< 4 -7 10) #false) (check-expect (<=< 4 4 4) #false) (check-expect (<=< 4 4.1 4.5) #true) (check-expect (<=< 100000 #x314d2ef361bcd159 +inf.0) #true) (check-expect (<=< -4.5 -4.1 -4) #true) ;(check-expect (<=< -4 -4.1 -4.5) #false) ; N.B. violates pre-condition: a > b. ; Therefore we comment out this test not because it the current-implementation fails it ; (it doesn't), but rather because including it is actually saying "any implementation ; that doesn't handle this test is an incorrect-implementation" -- which is not actually ; the case. ; overlap? : real,real,real,real, real,real,real,real -> boolean ; Does one rectangle overlap another? ; We represent a rectangle as four reals: the center x,y, width, height. ; ; For barely-touching rectangles, we use the rather odd convention that ; a rectangle is closed along its top and left sides, but open on its ; bottom and right. ; (This way, the squares (i,j,1,1) perfectly tile the plane, for i,j in N.) ; (define (overlap? x1 y1 w1 h1 x2 y2 w2 h2) ; Idea: is rect2's center inside rect1-enlarged-by-w2xh2 ? (and (<=< (- x1 (/ w1 2) (/ w2 2)) x2 (+ x1 (/ w1 2) (/ w2 2))) (<=< (- y1 (/ h1 2) (/ h2 2)) y2 (+ y1 (/ h1 2) (/ h2 2))) ; If I had `let`, I might write: #;(let* {[widths/2 (/ (+ w1 w2) 2)] …} … (<=< (- x1 widths/2) x2 (+ x1 widths/2)) …) ; ; Alternately: think of checking dist-btwn-centers, in each dimension: ; (< (abs (- x1 x2)) (+ (/ w1 2) (/ w2 2))) ; (< (abs (- y1 y2)) (+ (/ y1 2) (/ y2 2)))) ; (This doesn't quite get the half-open intervals though.) )) (check-expect (overlap? 5 5 10 10 6 6 2 2) #true) (check-expect (overlap? 5 5 10 10 15 6 20 2) #true) (check-expect (overlap? 5 5 10 10 6 15 2 20) #true) (check-expect (overlap? 5 5 10 10 15 15 20 20) #true) (check-expect (overlap? 5 5 10 10 16 16 2 2) #false) (check-expect (overlap? 5 5 10 10 25 25 20 20) #false) (check-expect (overlap? 5 5 10 10 -4 -4 2 2) #false) (check-expect (overlap? 5 5 10 10 -10 -10 20 20) #true) #| @author ibarland@radford.edu @version 2017-oct-22 License: CC-BY 4.0 -- you are free to share and adapt this file for any purpose, provided you include appropriate attribution. https://creativecommons.org/licenses/by/4.0/ https://creativecommons.org/licenses/by/4.0/legalcode Including a link to the *original* file satisifies "appropriate attribution". |#