1
#define __Geom_RECT_C__
4
* Pixel buffer rendering library
7
* Lauris Kaplinski <lauris@kaplinski.com>
9
* This code is in public domain
15
* \param r0 Rectangle.
16
* \param r1 Another rectangle.
17
* \param d Filled in with the intersection of r0 and r1.
24
Rect::Rect(const Point &p0, const Point &p1)
25
: _min(MIN(p0[X], p1[X]), MIN(p0[Y], p1[Y])),
26
_max(MAX(p0[X], p1[X]), MAX(p0[Y], p1[Y])) {}
28
/** returns the four corners of the rectangle in the correct winding order */
29
Point Rect::corner(unsigned i) const {
34
return Point(_max[X], _min[Y]);
38
return Point(_min[X], _max[Y]);
42
/** returns the midpoint of this rectangle */
43
Point Rect::midpoint() const {
44
return ( _min + _max ) / 2;
47
/** returns a vector from topleft to bottom right. */
48
Point Rect::dimensions() const {
52
/** Translates the rectangle by p. */
53
void Rect::offset(Point p) {
58
/** Makes this rectangle large enough to include the point p. */
59
void Rect::expandTo(Point p) {
60
for ( int i=0 ; i < 2 ; i++ ) {
61
_min[i] = MIN(_min[i], p[i]);
62
_max[i] = MAX(_max[i], p[i]);
66
/** Returns the set of points shared by both rectangles. */
67
Maybe<Rect> Rect::intersection(const Rect &a, const Rect &b) {
69
for ( int i=0 ; i < 2 ; i++ ) {
70
r._min[i] = MAX(a._min[i], b._min[i]);
71
r._max[i] = MIN(a._max[i], b._max[i]);
73
if ( r._min[i] > r._max[i] ) {
80
/** returns the smallest rectangle containing both rectangles */
81
Rect Rect::union_bounds(const Rect &a, const Rect &b) {
83
for ( int i=0; i < 2 ; i++ ) {
84
r._min[i] = MIN(a._min[i], b._min[i]);
85
r._max[i] = MAX(a._max[i], b._max[i]);
96
c-file-style:"stroustrup"
97
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
102
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :