Add region::overlap to for overlapping regions

This commit is contained in:
Danny Robson 2012-05-11 12:22:23 +10:00
parent 4dc7a3ca49
commit c1ba948b20
2 changed files with 21 additions and 2 deletions

View File

@ -35,7 +35,8 @@ region<T>::region (T _x, T _y, T _w, T _h):
y (_y), y (_y),
w (_w), w (_w),
h (_h) h (_h)
{ ; } {
}
template <typename T> template <typename T>
@ -77,7 +78,7 @@ region<T>::base (void) const {
template <typename T> template <typename T>
point point
region<T>::centre (void) const { 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); cy = y + static_cast<T>(h / 2.0);
return { cx, cy, 0.0 }; return { cx, cy, 0.0 };
@ -107,12 +108,28 @@ region<T>::contains (const point& p) const {
template <typename T> template <typename T>
bool bool
region<T>::overlaps (const region<T> &rhs) const { region<T>::overlaps (const region<T> &rhs) const {
//return !overlap (rhs).empty ();
return x < rhs.x + rhs.w && return x < rhs.x + rhs.w &&
x + w > rhs.x && x + w > rhs.x &&
y < rhs.y + rhs.h && y < rhs.y + rhs.h &&
y + h > rhs.y; 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> template <typename T>
bool bool
region<T>::operator ==(const region& rhs) const region<T>::operator ==(const region& rhs) const

View File

@ -48,6 +48,8 @@ namespace util {
bool contains (const point&) const; // exclusive of borders bool contains (const point&) const; // exclusive of borders
bool overlaps (const region<T>&) 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;
bool operator !=(const region<T>& rhs) const bool operator !=(const region<T>& rhs) const
{ return !(*this == rhs); } { return !(*this == rhs); }