typedef struct rect rect;
struct rect {
int x, y; /* top-left point inside rectangle */
int width, height; /* pixel width and height of rectangle */
};
rect rect(int left, int top, int width, int height); rect rpt(point min, point max); point topleft(rect r); /* top-left point inside rectangle */ point topright(rect r); /* top-right point inside rectangle */ point bottomleft(rect r); /* bottom-left point inside rect */ point bottomright(rect r); /* bottom-right point inside rect */ rect raddpt(rect r, point p); /* add the point p to r */ rect rsubpt(rect r, point p); /* subtract p from r */ rect rmul(rect r, int i); /* multiply r by a constant */ rect rdiv(rect r, int i); /* divide r by a constant */ rect growr(rect r, int w, int h); /* grow r outwards */ rect insetr(rect r, int i); /* inset r by i pixels */ rect rcenter(rect r1, rect r2); /* center r1 on r2 */ int equalr(rect r1, rect r2); /* is r1 equal to r2? */ int rinr(rect r1, rect r2); /* is r1 inside r2? */ int rxr(rect r1, rect r2); /* do r1 and r2 intersect? */ rect clipr(rect r1, rect r2); /* clip r1 inside r2 */ rect rcanon(rect r); /* make r have positive size */
A rect defines a rectangular area. The x and y co-ordinates define the top-left point within the rectangle, and the rectangle's width and height are recorded in pixels. The point (x+width,y+height) will thus be outside the rectangle.

A new rectangle can be returned using rect(x,y,width,height).
A rectangle can also be created by specifying the top-left and bottom-right points using rpt(min, max). The parameters min and max refer to the top-left point inside the rectangle, and the bottom-right point outside the rectangle, respectively. Hence, the return value will be rect(min.x,min.y,max.x-min.x,max.y-min.y).
The top-left point can be found using the function topleft(r), while the bottom-right point inside the rectangle can be found using bottomright(r): this will be the point (x+width-1,y+height-1). Similarly the points returned by topright and bottomleft are within the rectangle.
A point can be added to a rectangle using raddpt, which has the effect of moving the rectangle: raddpt(r,p) returns rect(r.x+p.x, r.x+p.y, r.width, r.height). Subtracting a point using rsubpt moves the rectangle in the other direction, subtracting the point's co-ordinates from the rectangle's.
The rmul and rdiv functions multiply each of the co-ordinates of the supplied rectangle by a constant, in a similar way to the operation of mulpt and divpt.
A rectangle can be made larger using the growr function: growr(r,w,h) returns the rectangle rect(r.x-w,r.y-h,r.width+w+w,r.height+h+h). The insetr function returns a rectangle which is inset from the given rectangle by the specified number of pixels all the way around. So insetr(r,i) is equivalent to rect(r.x+i,r.y+i,r.width-i-i,r.height-i-i).
A rectangle can be centered within another rectangle using the rcenter function: rcenter(r1,r2) will return a rectangle with the same size as r1, but centered within r2.
The equalr function compares two rectangles, returning non-zero if they are equal, zero otherwise.
Calling rinr(r1, r2) returns non-zero only if r1 is wholly contained within r2. By contrast, rxr(r1, r2) returns non-zero if any part of r1 intersects with r2.
The clipr function clips a rectangle so that it is within another: clipr(r1, r2) will clip r1 to be within r2 and return r1, unless r1 does not overlap r2 at all, in which case it will return r1 unchanged.
The rcanon function converts the supplied rectangle to canonical form; the width and height of the returned rectangle will be positive, and the area and location of the rectangle will remain unchanged.
(NB: If using C++, some of the above functions may be replaced with operator overloaded functions. Addition, subtraction and comparison of points and rectangles can be performed using the +, -, +=, -=, ==, != operators.)