My first representation define a rectangle as a point with two vectors as described in the image.
I used vectors from \(^2\) for my definition of a rectangle so i need to write the constructor and the selectors needed.
We can represent the data structure of a rectangle with the box-and-pointer notation.
Now we can define a procedure to compute the norm of the vectors and one procedure to directly find the length and the width of a rectangle.
We can finally write the procedures to find the perimeter and the area of a rectangle.
The second way of representing a rectangle is with three points (in clockwise or counterclockwise order). This representation is less elegant in my opinion so I will not explain it as my first representation.
So for this representation we need a new constructor and new selectors for our rectangles.
Now we need to change the
width procedures according to the new representation, I choose to use my already defined
(define (make-vector-from-points p1 p2) (make-vector (abs (- (x-point p1) (x-point p2))) (abs (- (y-point p1) (y-point p2))))) (define (length rectangle) (let ((v1 (make-vector-from-points (p1 rectangle) (p2 rectangle)))) (vector-norm v1))) (define (width rectangle) (let ((v2 (make-vector-from-points (p2 rectangle) (p3 rectangle)))) (vector-norm v2)))
And now we can define the perimeter and the area.
As you can see these two last procedures are the same as the
perimeter used in the first representation of a rectangle because we used different layers of abstractions.