diff --git a/region.cpp b/region.cpp index b0948cc5..9ae394b3 100644 --- a/region.cpp +++ b/region.cpp @@ -35,7 +35,8 @@ region::region (T _x, T _y, T _w, T _h): y (_y), w (_w), h (_h) -{ ; } +{ +} template @@ -77,7 +78,7 @@ region::base (void) const { template point region::centre (void) const { - double cx = x + static_cast(w / 2.0), + double cx = x + static_cast(w / 2.0), cy = y + static_cast(h / 2.0); return { cx, cy, 0.0 }; @@ -107,12 +108,28 @@ region::contains (const point& p) const { template bool region::overlaps (const region &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 +region +region::overlap (const region &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 (newx1, newy1, newx2 - newx1, newy2 - newy1); + +} + + template bool region::operator ==(const region& rhs) const diff --git a/region.hpp b/region.hpp index 43a1ec55..68a59a3b 100644 --- a/region.hpp +++ b/region.hpp @@ -48,6 +48,8 @@ namespace util { bool contains (const point&) const; // exclusive of borders bool overlaps (const region&) const; // exclusive of borders + region overlap (const region&) const; + bool operator ==(const region& rhs) const; bool operator !=(const region& rhs) const { return !(*this == rhs); }