Smatchcube's website 🌍


Exercise 2.3

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 length and width procedures according to the new representation, I choose to use my already defined vector-norm.

And now we can define the perimeter and the area.

As you can see these two last procedures are the same as the area and perimeter used in the first representation of a rectangle because we used different layers of abstractions.