;; 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 20.4.3) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
; Worked exercise 20.4.3
; distance-to-top-left : posn -> number
(check-within (distance-to-top-left (make-posn 0 0)) 0 .1)
(check-within (distance-to-top-left (make-posn 6 0)) 6 .1)
(check-within (distance-to-top-left (make-posn 0 4.3)) 4.3 .1)
(check-within (distance-to-top-left (make-posn 3 4)) 5 .1)
; 3^2 + 4^2 = 9 + 16 = 25 = 5^2
(check-within (distance-to-top-left (make-posn 4 7)) 8.1 .1)
; 4^2 + 7^2 = 16 + 49 = 65 > 8^2
(define (distance-to-top-left the-point)
; the-point a posn
; (posn-x the-point) a number (x)
; (posn-y the-point) a number (y)
; (* (posn-x the-point) (posn-x the-point)) a number (x^2)
; (* (posn-y the-point) (posn-y the-point)) a number (y^2)
; (+ (* (posn-x the-point) (posn-x the-point))
; (* (posn-y the-point) (posn-y the-point)))
; a number (x^2 + y^2)
(sqrt (+ (* (posn-x the-point) (posn-x the-point))
(* (posn-y the-point) (posn-y the-point))))
)