Rectangle
A rectangle has four sides; the angle between each side is 90 degrees. There are several ways to represent a rectangle: using a Min and Max point, using a Center and half-extents, or using a Position and a Size:
Getting ready
We are going to implement our rectangle structure using the origin and Size representation. However, having the Min and Max representation of a rectangle is often useful. For this reason, we are going to implement helper functions to get the Min and Max points of a rectangle, and we will to make a rectangle from a Min and Max pair.
How to do it…
Follow these steps to implement a two-dimensional rectangle and all of the support functions we will need to work with the rectangle:
Add the declaration of the
Rectangle2D
structure toGeometry2D.h
:typedef struct Rectangle2D { Point2D origin; vec2 size; inline Rectangle2D() : size(1, 1) { } inline Rectangle2D(const Point2D& o, const vec2& s) : origin(o), size(s) { } } Rectangle2D;
Add the declaration...