~lib2geom-hackers/lib2geom/trunk

« back to all changes in this revision

Viewing changes to rect.cpp

  • Committer: njh
  • Date: 2006-05-22 11:50:24 UTC
  • Revision ID: svn-v4:4601daaa-0314-0410-9a8b-c964a3c23b6b:trunk/lib2geom:1
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define __Geom_RECT_C__
 
2
 
 
3
/*
 
4
 * Pixel buffer rendering library
 
5
 *
 
6
 * Authors:
 
7
 *   Lauris Kaplinski <lauris@kaplinski.com>
 
8
 *
 
9
 * This code is in public domain
 
10
 */
 
11
 
 
12
#include "rect-l.h"
 
13
 
 
14
/**
 
15
 *    \param r0 Rectangle.
 
16
 *    \param r1 Another rectangle.
 
17
 *    \param d Filled in with the intersection of r0 and r1.
 
18
 *    \return d.
 
19
 */
 
20
 
 
21
 
 
22
namespace Geom {
 
23
 
 
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])) {}
 
27
 
 
28
/** returns the four corners of the rectangle in the correct winding order */
 
29
Point Rect::corner(unsigned i) const {
 
30
        switch (i % 4) {
 
31
        case 0:
 
32
                return _min;
 
33
        case 1:
 
34
                return Point(_max[X], _min[Y]);
 
35
        case 2:
 
36
                return _max;
 
37
        default: /* i.e. 3 */
 
38
                return Point(_min[X], _max[Y]);
 
39
        }
 
40
}
 
41
 
 
42
/** returns the midpoint of this rectangle */
 
43
Point Rect::midpoint() const {
 
44
        return ( _min + _max ) / 2;
 
45
}
 
46
 
 
47
/** returns a vector from topleft to bottom right. */
 
48
Point Rect::dimensions() const {
 
49
        return _max - _min;
 
50
}
 
51
 
 
52
/** Translates the rectangle by p. */
 
53
void Rect::offset(Point p) {
 
54
        _min += p;
 
55
        _max += p;
 
56
}
 
57
 
 
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]);
 
63
        }
 
64
}
 
65
 
 
66
/** Returns the set of points shared by both rectangles. */
 
67
Maybe<Rect> Rect::intersection(const Rect &a, const Rect &b) {
 
68
        Rect r;
 
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]);
 
72
 
 
73
                if ( r._min[i] > r._max[i] ) {
 
74
                        return Nothing();
 
75
                }
 
76
        }
 
77
        return r;
 
78
}
 
79
 
 
80
/** returns the smallest rectangle containing both rectangles */
 
81
Rect Rect::union_bounds(const Rect &a, const Rect &b) {
 
82
        Rect r;
 
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]);
 
86
        }
 
87
        return r;
 
88
}
 
89
 
 
90
}  // namespace Geom
 
91
 
 
92
 
 
93
/*
 
94
  Local Variables:
 
95
  mode:c++
 
96
  c-file-style:"stroustrup"
 
97
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
98
  indent-tabs-mode:nil
 
99
  fill-column:99
 
100
  End:
 
101
*/
 
102
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :