#reader(lib "htdp-beginner-reader.ss" "lang")((modname kathi-finder-single) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
(define WIDTH 320)
(define HEIGHT 480)
(define-struct loc (lat long))
(define-struct place (name loc radius))
(define (mile->meter miles)
(* miles 1609.344))
(define WPI-PLACE
(make-place "WPI" (make-loc 42.272824 -71.808207) 500))
(define WPI-PARKING-PLACE
(make-place "WPI Parking" (make-loc 42.2737222 -71.8058627) 50))
(define WORCESTER-PLACE
(make-place "Worcester" (make-loc 42.274514 -71.798744) (mile->meter 3)))
(define ALL-PLACES
(list WPI-PLACE
WPI-PARKING-PLACE
WORCESTER-PLACE))
(define ADDRESS "5554")
(define initial-world (make-loc 0 0))
(define (loc->string w)
(string-append "("
(number->string (loc-lat w))
", "
(number->string (loc-long w))
")"))
(define (change-location w lat long)
(make-loc lat long))
(define (identity w)
w)
(define (current-place w)
(cond [(empty? (find-places ALL-PLACES w))
(make-place "Unknown" w 0)]
[else
(choose-smallest
(find-places ALL-PLACES w))]))
(define (send-report w)
(make-effect:send-sms ADDRESS
(string-append (description w)
"\n"
(maps-url (place-loc (current-place w))))))
(define (maps-url a-loc)
(string-append "http://maps.google.com/maps?q="
(number->string
(exact->inexact (loc-lat a-loc)))
",+"
(number->string
(exact->inexact (loc-long a-loc)))
"&iwloc=A&hl=en"))
(define (description w)
(place-name (current-place w)))
(define (choose-smallest places)
(cond
[(empty? (rest places))
(first places)]
[(< (place-radius (first places)) (place-radius (second places)))
(choose-smallest (cons (first places) (rest (rest places))))]
[else
(choose-smallest (rest places))]))
(define (find-places places a-loc)
(cond
[(empty? places)
empty]
[(place-matches? (first places) a-loc)
(cons (first places) (find-places (rest places) a-loc))]
[else
(find-places (rest places) a-loc)]))
(define (place-matches? a-place a-loc)
(<= (location-distance (loc-lat a-loc)
(loc-long a-loc)
(loc-lat (place-loc a-place))
(loc-long (place-loc a-place)))
(place-radius a-place)))
(define (draw w)
(list (js-div)
(list (js-p '(("id" "aPara")))
(list (js-text (description w)))
(list (js-text " "))
(list (js-text (loc->string w))))))
(define (draw-css w)
'(("aPara" ("font-size" "30px"))))
(define tick-delay 10 (* 5 60))
(js-big-bang initial-world
'()
(on-draw draw draw-css)
(on-tick* tick-delay identity send-report)
(on-location-change change-location))