38 #ifndef DRAIN_RECTANGLE2_H_
39 #define DRAIN_RECTANGLE2_H_
42 #include <drain/UniTuple.h>
46 #include <drain/util/Point.h>
47 #include <drain/Type.h>
72 Rectangle(T x=0, T y=0, T x2=0, T y2=0) : lowerLeft(this->tuple(), 0), upperRight(this->tuple(), 2) {
73 this->set(x, y, x2, y2);
83 lowerLeft.setLocation(ll);
84 upperRight.setLocation(ur);
90 this->set(rect.tuple());
96 T
getWidth()
const {
return (this->upperRight.x - this->lowerLeft.x); };
101 return (this->upperRight.y - this->lowerLeft.y);
120 p.x = (lowerLeft.x + upperRight.x)/divider;
121 p.y = (lowerLeft.y + upperRight.y)/divider;
143 bool isInside(
const T &x,
const T &y)
const {
144 return ((x>this->lowerLeft.x) && (x<this->upperRight.x) && (y>this->lowerLeft.y) && (y<this->upperRight.y));
148 bool isOverLapping(
const Rectangle &r)
const {
149 const bool xOverLap = !((r.upperRight.x < this->lowerLeft.x) || (r.lowerLeft.x > this->upperRight.x));
150 const bool yOverLap = !((r.upperRight.y < this->lowerLeft.y) || (r.lowerLeft.y > this->upperRight.y));
151 return (xOverLap && yOverLap);
167 bool limit(
const T & lowerBound,
const T & upperBound, T & x){
169 if (lowerBound > upperBound){
170 return limit(upperBound, lowerBound, x);
178 else if (x > upperBound){
194 result |= limit(bounds.lowerLeft.x, bounds.upperRight.x, this->lowerLeft.x);
195 result |= limit(bounds.lowerLeft.x, bounds.upperRight.x, this->upperRight.x);
196 result |= limit(bounds.lowerLeft.y, bounds.upperRight.y, this->lowerLeft.y);
197 result |= limit(bounds.lowerLeft.y, bounds.upperRight.y, this->upperRight.y);
205 this->lowerLeft.x = std::min(this->lowerLeft.x,r.lowerLeft.x);
206 this->lowerLeft.y = std::min(this->lowerLeft.y,r.lowerLeft.y);
207 this->upperRight.x = std::max(this->upperRight.x,r.upperRight.x);
208 this->upperRight.y = std::max(this->upperRight.y,r.upperRight.y);
215 this->lowerLeft.x = std::max(this->lowerLeft.x,r.lowerLeft.x);
216 this->lowerLeft.y = std::max(this->lowerLeft.y,r.lowerLeft.y);
217 this->upperRight.x = std::min(this->upperRight.x,r.upperRight.x);
218 this->upperRight.y = std::min(this->upperRight.y,r.upperRight.y);
224 ostr << r.lowerLeft.x <<
',' << r.lowerLeft.y <<
' ' << r.upperRight.x <<
',' << r.upperRight.y;
231 DRAIN_TYPENAME_T(Rectangle,T);
tuplebase_t & assignSequence(T &sequence, bool LENIENT=false)
Proposed for tuples only; derived classes should not shadow this.
Definition: TupleBase.h:244
T & toSequence(T &sequence) const
Copy elements to a Sequence, like stl::list, stl::set or stl::vector.
Definition: TupleBase.h:160
Tuple of N elements of type T.
Definition: UniTuple.h:65
Definition: DataSelector.cpp:1277
Rectange defined through lower left and upper right coordinates.
Definition: Rectangle.h:65
Rectangle(const Point2D< T > &ll, const Point2D< T > &ur)
Constructor with corner points.
Definition: Rectangle.h:82
Rectangle(const Rectangle &r)
Copy constructor.
Definition: Rectangle.h:77
bool crop(const Rectangle< T > &r)
This becomes the intersection of r and this.
Definition: Rectangle.h:190
bool limit(const T &lowerBound, const T &upperBound, T &x)
Limits x between interval [lowerBound, upperBound].
Definition: Rectangle.h:167
bool empty() const
Return true if the area is zero.
Definition: Rectangle.h:111
T getWidth() const
Return the difference of X coordinates.
Definition: Rectangle.h:96
Rectangle(T x=0, T y=0, T x2=0, T y2=0)
Basic constructor.
Definition: Rectangle.h:72
T getHeight() const
Return the difference of Y coordinates.
Definition: Rectangle.h:100
void contract(const Rectangle &r)
The instance reduces to itse intersection with r.
Definition: Rectangle.h:213
std::vector< T > toVector() const
Write corner points to a vector [llX, llY, urX, urY].
Definition: Rectangle.h:156
void extend(const Rectangle &r)
The instance extends to its union with r.
Definition: Rectangle.h:203
Rectangle< T > & operator=(const Rectangle< T > &rect)
Assign rectangle of the same type.
Definition: Rectangle.h:89
void getCenter(drain::Point2D< T > &p) const
Return the center point.
Definition: Rectangle.h:118