sicp/2_3.sch

41 lines
1.2 KiB
Scheme

(load "2_2.sch")
(define (make-rect-corners top-left bottom-right)
(let ((top-right
(make-point (x-point bottom-right) (y-point top-left)))
(bottom-left
(make-point (x-point top-left) (y-point bottom-right))))
(list top-left top-right bottom-right bottom-left)))
(define (make-rect-size bottom-left width height)
(let ((top-left
(make-point (x-point bottom-left)
(+ height (y-point bottom-left))))
(top-right
(make-point (+ width (x-point bottom-left))
(+ height (y-point bottom-left))))
(bottom-right
(make-point (+ width (x-point bottom-left))
(y-point bottom-left))))
(list top-left top-right bottom-right bottom-left)))
(define (rect-top-left r) (car r))
(define (rect-top-right r) (cadr r))
(define (rect-bottom-right r) (caddr r))
(define (rect-bottom-left r) (cadddr r))
(define (perimeter r)
(let ((tl (rect-top-left r))
(br (rect-bottom-right r)))
(+
(* 2 (abs (- (x-point br) (x-point tl))))
(* 2 (abs (- (y-point br) (y-point tl)))))))
(define (area r)
(let ((tl (rect-top-left r))
(br (rect-bottom-right r)))
(*
(abs (- (x-point tl) (x-point br)))
(abs (- (y-point tl) (y-point br))))))