45 #include <boost/optional/optional.hpp>
49 typedef D2<Interval> Rect;
51 Rect unify(
const Rect &,
const Rect &);
58 D2<Interval>() { f[X] = f[Y] = Interval(0, 0); }
60 D2<Interval>(Interval
const &a, Interval
const &b) {
65 D2<Interval>(Point
const & a, Point
const & b) {
66 f[X] = Interval(a[X], b[X]);
67 f[Y] = Interval(a[Y], b[Y]);
70 inline Interval& operator[](
unsigned i) {
return f[i]; }
71 inline Interval
const & operator[](
unsigned i)
const {
return f[i]; }
73 inline Point min()
const {
return Point(f[X].min(), f[Y].min()); }
74 inline Point max()
const {
return Point(f[X].max(), f[Y].max()); }
78 Point corner(
unsigned i)
const {
80 case 0:
return Point(f[X].min(), f[Y].min());
81 case 1:
return Point(f[X].max(), f[Y].min());
82 case 2:
return Point(f[X].max(), f[Y].max());
83 default:
return Point(f[X].min(), f[Y].max());
88 inline double top()
const {
return f[Y].min(); }
89 inline double bottom()
const {
return f[Y].max(); }
90 inline double left()
const {
return f[X].min(); }
91 inline double right()
const {
return f[X].max(); }
93 inline double width()
const {
return f[X].extent(); }
94 inline double height()
const {
return f[Y].extent(); }
97 inline Point dimensions()
const {
return Point(f[X].extent(), f[Y].extent()); }
98 inline Point midpoint()
const {
return Point(f[X].middle(), f[Y].middle()); }
100 inline double area()
const {
return f[X].extent() * f[Y].extent(); }
101 inline double maxExtent()
const {
return std::max(f[X].extent(), f[Y].extent()); }
103 inline bool isEmpty()
const {
104 return f[X].isEmpty() && f[Y].isEmpty();
106 inline bool intersects(Rect
const &r)
const {
107 return f[X].intersects(r[X]) && f[Y].intersects(r[Y]);
109 inline bool contains(Rect
const &r)
const {
110 return f[X].contains(r[X]) && f[Y].contains(r[Y]);
112 inline bool contains(Point
const &p)
const {
113 return f[X].contains(p[X]) && f[Y].contains(p[Y]);
116 inline void expandTo(Point p) {
117 f[X].extendTo(p[X]); f[Y].extendTo(p[Y]);
119 inline void unionWith(Rect
const &b) {
120 f[X].unionWith(b[X]); f[Y].unionWith(b[Y]);
123 inline void expandBy(
double amnt) {
124 f[X].expandBy(amnt); f[Y].expandBy(amnt);
126 inline void expandBy(Point
const p) {
127 f[X].expandBy(p[X]); f[Y].expandBy(p[Y]);
132 inline Rect operator*(Matrix
const m)
const {
133 return unify(Rect(corner(0) * m, corner(2) * m),
134 Rect(corner(1) * m, corner(3) * m));
138 inline Rect unify(Rect
const & a, Rect
const & b) {
139 return Rect(unify(a[X], b[X]), unify(a[Y], b[Y]));
142 inline Rect union_list(std::vector<Rect>
const &r) {
143 if(r.empty())
return Rect(Interval(0,0), Interval(0,0));
145 for(
unsigned i = 1; i < r.size(); i++)
150 inline boost::optional<Rect> intersect(Rect
const & a, Rect
const & b) {
151 boost::optional<Interval> x = intersect(a[X], b[X]);
152 boost::optional<Interval> y = intersect(a[Y], b[Y]);
153 return x && y ? boost::optional<Rect>(Rect(*x, *y)) : boost::optional<Rect>();