Add region::overlap to for overlapping regions
This commit is contained in:
parent
4dc7a3ca49
commit
c1ba948b20
21
region.cpp
21
region.cpp
@ -35,7 +35,8 @@ region<T>::region (T _x, T _y, T _w, T _h):
|
||||
y (_y),
|
||||
w (_w),
|
||||
h (_h)
|
||||
{ ; }
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
@ -77,7 +78,7 @@ region<T>::base (void) const {
|
||||
template <typename T>
|
||||
point
|
||||
region<T>::centre (void) const {
|
||||
double cx = x + static_cast<T>(w / 2.0),
|
||||
double cx = x + static_cast<T>(w / 2.0),
|
||||
cy = y + static_cast<T>(h / 2.0);
|
||||
|
||||
return { cx, cy, 0.0 };
|
||||
@ -107,12 +108,28 @@ region<T>::contains (const point& p) const {
|
||||
template <typename T>
|
||||
bool
|
||||
region<T>::overlaps (const region<T> &rhs) const {
|
||||
//return !overlap (rhs).empty ();
|
||||
|
||||
return x < rhs.x + rhs.w &&
|
||||
x + w > rhs.x &&
|
||||
y < rhs.y + rhs.h &&
|
||||
y + h > rhs.y;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
region<T>
|
||||
region<T>::overlap (const region<T> &rhs) const {
|
||||
double newx1 = max (x, rhs.x),
|
||||
newy1 = max (y, rhs.y),
|
||||
newx2 = min (x + w, rhs.x + w),
|
||||
newy2 = min (y + h, rhs.y + h);
|
||||
|
||||
return region<T> (newx1, newy1, newx2 - newx1, newy2 - newy1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool
|
||||
region<T>::operator ==(const region& rhs) const
|
||||
|
@ -48,6 +48,8 @@ namespace util {
|
||||
bool contains (const point&) const; // exclusive of borders
|
||||
bool overlaps (const region<T>&) const; // exclusive of borders
|
||||
|
||||
region overlap (const region<T>&) const;
|
||||
|
||||
bool operator ==(const region<T>& rhs) const;
|
||||
bool operator !=(const region<T>& rhs) const
|
||||
{ return !(*this == rhs); }
|
||||
|
Loading…
Reference in New Issue
Block a user