added RectI::overlap_rect

This commit is contained in:
Noel Berry 2021-04-03 22:24:51 -07:00
parent 14a53c0f3a
commit aa6efb23b1
2 changed files with 22 additions and 0 deletions

View File

@ -31,6 +31,8 @@ namespace Blah
Point bottom_right() const; Point bottom_right() const;
bool overlaps(const RectI& other) const; bool overlaps(const RectI& other) const;
RectI overlap_rect(const Rect& against) const;
bool contains(const Point& pt) const; bool contains(const Point& pt) const;
bool contains(const Vec2& pt) const; bool contains(const Vec2& pt) const;

View File

@ -2,6 +2,7 @@
#include <blah/math/rect.h> #include <blah/math/rect.h>
#include <blah/math/point.h> #include <blah/math/point.h>
#include <blah/math/vec2.h> #include <blah/math/vec2.h>
#include <blah/math/calc.h>
using namespace Blah; using namespace Blah;
@ -89,6 +90,25 @@ bool RectI::overlaps(const RectI& other) const
&& other.y < y + h; && 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 bool RectI::contains(const Point& point) const
{ {
return point.x >= x && point.x < x + w && point.y >= y && point.y < y + h; return point.x >= x && point.x < x + w && point.y >= y && point.y < y + h;