diff --git a/include/blah/math/rectI.h b/include/blah/math/rectI.h index fb4e10e..d05acda 100644 --- a/include/blah/math/rectI.h +++ b/include/blah/math/rectI.h @@ -31,6 +31,8 @@ namespace Blah Point bottom_right() const; bool overlaps(const RectI& other) const; + RectI overlap_rect(const Rect& against) const; + bool contains(const Point& pt) const; bool contains(const Vec2& pt) const; diff --git a/src/math/rectI.cpp b/src/math/rectI.cpp index fd4b28a..2262f38 100644 --- a/src/math/rectI.cpp +++ b/src/math/rectI.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace Blah; @@ -89,6 +90,25 @@ bool RectI::overlaps(const RectI& other) const && other.y < y + h; } +RectI RectI::overlap_rect(const Rect& against) const +{ + RectI result(0, 0, 0, 0); + + if (x + w >= against.x && x < against.x + against.w) + { + result.x = Calc::max(x, against.x); + result.w = Calc::min(x + w, against.x + against.w) - result.x; + } + + if (y + h >= against.y && y < against.y + against.h) + { + result.y = Calc::max(y, against.y); + result.h = Calc::min(y + h, against.y + against.h) - result.y; + } + + return result; +} + bool RectI::contains(const Point& point) const { return point.x >= x && point.x < x + w && point.y >= y && point.y < y + h;