Code

CodingStyle: const placement
[inkscape.git] / src / libnr / nr-rect-l.h
1 #ifndef SEEN_NR_RECT_L_H
2 #define SEEN_NR_RECT_L_H
4 #include <libnr/nr-i-coord.h>
6 struct NRRectL {
7     NR::ICoord x0, y0, x1, y1;
8 };
10 #include <libnr/nr-rect.h>
11 #include <libnr/nr-point-l.h>
13 namespace NR {
16 class IRect {
17 public:
18     IRect(const NRRectL& r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
19     IRect(const IRect& r) : _min(r._min), _max(r._max) {}
20     IRect(const IPoint &p0, const IPoint &p1);
21     
22     /** as not all Rects are representable by IRects this gives the smallest IRect that contains
23      * r. */
24     IRect(const Rect& r);
25     
26     operator Rect() {
27         return Rect(Point(_min), Point(_max));
28     }
30     const IPoint &min() const { return _min; }
31     const IPoint &max() const { return _max; }
32     
33     /** returns a vector from min to max. */
34     IPoint dimensions() const;
35     
36     /** does this rectangle have zero area? */
37     bool isEmpty() const {
38         return isEmpty<X>() && isEmpty<Y>();
39     }
40     
41     bool intersects(const IRect &r) const {
42         return intersects<X>(r) && intersects<Y>(r);
43     }
44     bool contains(const IRect &r) const {
45         return contains<X>(r) && contains<Y>(r);
46     }
47     bool contains(const IPoint &p) const {
48         return contains<X>(p) && contains<Y>(p);
49     }
50     
51     ICoord maxExtent() const {
52         return MAX(extent<X>(), extent<Y>());
53     }
55     ICoord extent(Dim2 axis) const {
56         switch (axis) {
57         case X: return extent<X>();
58         case Y: return extent<Y>();
59         };
60     }
62     ICoord extent(unsigned i) const throw(std::out_of_range) {
63         switch (i) {
64         case 0: return extent<X>();
65         case 1: return extent<Y>();
66         default: throw std::out_of_range("Dimension out of range");
67         };
68     }
70     /** Translates the rectangle by p. */
71     void offset(IPoint p);
72         
73     /** Makes this rectangle large enough to include the point p. */
74     void expandTo(IPoint p);
76     /** Makes this rectangle large enough to include the rectangle r. */
77     void expandTo(const IRect &r);
78         
79     /** Returns the set of points shared by both rectangles. */
80     static Maybe<IRect> intersection(const IRect &a, const IRect &b);
82     /** Returns the smallest rectangle that encloses both rectangles. */
83     static IRect union_bounds(const IRect &a, const IRect &b);
85 private:
86     IRect() {}
88     template <NR::Dim2 axis>
89     ICoord extent() const {
90         return _max[axis] - _min[axis];
91     }
93     template <Dim2 axis>
94     bool isEmpty() const {
95         return !( _min[axis] < _max[axis] );
96     }
98     template <Dim2 axis>
99     bool intersects(const IRect &r) const {
100         return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
101     }
103     template <Dim2 axis>
104     bool contains(const IRect &r) const {
105         return contains(r._min) && contains(r._max);
106     }
108     template <Dim2 axis>
109     bool contains(const IPoint &p) const {
110         return p[axis] >= _min[axis] && p[axis] <= _max[axis];
111     }
113     IPoint _min, _max;
114 };
118 }  // namespace NR
120 #endif /* !SEEN_NR_RECT_L_H */
122 /*
123   Local Variables:
124   mode:c++
125   c-file-style:"stroustrup"
126   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
127   indent-tabs-mode:nil
128   fill-column:99
129   End:
130 */
131 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :