1
#ifndef SEEN_NR_RECT_L_H
2
#define SEEN_NR_RECT_L_H
4
#include <libnr/nr-i-coord.h>
5
#include <boost/optional.hpp>
6
#include <libnr/nr-rect.h>
7
#include <libnr/nr-point-l.h>
10
boost::optional<NR::Rect> upgrade() const;
11
NR::ICoord x0, y0, x1, y1;
20
IRect(const NRRectL& r) : _min(r.x0, r.y0), _max(r.x1, r.y1) {}
21
IRect(const IRect& r) : _min(r._min), _max(r._max) {}
22
IRect(const IPoint &p0, const IPoint &p1) : _min(p0), _max(p1) {}
24
/** as not all Rects are representable by IRects this gives the smallest IRect that contains
29
return Rect(Point(_min), Point(_max));
32
const IPoint &min() const { return _min; }
33
const IPoint &max() const { return _max; }
35
/** returns a vector from min to max. */
36
IPoint dimensions() const;
38
/** does this rectangle have zero area? */
39
bool isEmpty() const {
40
return isEmpty<X>() && isEmpty<Y>();
43
bool intersects(const IRect &r) const {
44
return intersects<X>(r) && intersects<Y>(r);
46
bool contains(const IRect &r) const {
47
return contains<X>(r) && contains<Y>(r);
49
bool contains(const IPoint &p) const {
50
return contains<X>(p) && contains<Y>(p);
53
ICoord maxExtent() const {
54
return MAX(extent<X>(), extent<Y>());
57
ICoord extent(Dim2 axis) const {
59
case X: return extent<X>();
60
case Y: return extent<Y>();
64
ICoord extent(unsigned i) const throw(std::out_of_range) {
66
case 0: return extent<X>();
67
case 1: return extent<Y>();
68
default: throw std::out_of_range("Dimension out of range");
72
/** Translates the rectangle by p. */
73
void offset(IPoint p);
75
/** Makes this rectangle large enough to include the point p. */
76
void expandTo(IPoint p);
78
/** Makes this rectangle large enough to include the rectangle r. */
79
void expandTo(const IRect &r);
81
/** Returns the set of points shared by both rectangles. */
82
static boost::optional<IRect> intersection(const IRect &a, const IRect &b);
84
/** Returns the smallest rectangle that encloses both rectangles. */
85
static IRect union_bounds(const IRect &a, const IRect &b);
87
bool operator==(const IRect &other) const {
88
return (min() == other.min()) && (max() == other.max());
91
bool operator!=(const IRect &other) const {
92
return (min() != other.min()) || (max() != other.max());
98
template <NR::Dim2 axis>
99
ICoord extent() const {
100
return _max[axis] - _min[axis];
104
bool isEmpty() const {
105
return !( _min[axis] < _max[axis] );
109
bool intersects(const IRect &r) const {
110
return _max[axis] >= r._min[axis] && _min[axis] <= r._max[axis];
114
bool contains(const IRect &r) const {
115
return contains(r._min) && contains(r._max);
119
bool contains(const IPoint &p) const {
120
return p[axis] >= _min[axis] && p[axis] <= _max[axis];
130
#endif /* !SEEN_NR_RECT_L_H */
135
c-file-style:"stroustrup"
136
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
141
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :