diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 9b739e9..55d41c7 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -129,6 +129,10 @@ bool Rect::Contains(const Point& point) const { return !(point.x < x || point.y < y || point.x > GetX2() || point.y > GetY2()); } +bool Rect::Contains(const Rect& rect) const { + return rect.x >= x && rect.y >= y && rect.GetX2() <= GetX2() && rect.GetY2() <= GetY2(); +} + Rect Rect::operator+(const Point& offset) const { return Rect(x + offset.x, y + offset.y, w, h); } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index 4439396..58305d3 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -253,6 +253,16 @@ public: //////////////////////////////////////////////////////////// bool Contains(const Point& point) const; + //////////////////////////////////////////////////////////// + /// \brief Check whether the rect contains another rect + /// + /// \param rect Rect to check + /// + /// \returns True if the checked rect is contained in this rect + /// + //////////////////////////////////////////////////////////// + bool Contains(const Rect& rect) const; + //////////////////////////////////////////////////////////// /// \brief Get rectangle moved by a given offset /// diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 9e8597d..beaac28 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -155,6 +155,15 @@ BEGIN_TEST() EXPECT_TRUE(!r.Contains(Point(10, 19))); EXPECT_TRUE(!r.Contains(Point(15, 20))); EXPECT_TRUE(!r.Contains(Point(10, 25))); + + // Rect contains rect + EXPECT_TRUE(r.Contains(r)); + EXPECT_TRUE(r.Contains(Rect(11, 21, 3, 3))); + + EXPECT_TRUE(!r.Contains(Rect(9, 20, 5, 5))); + EXPECT_TRUE(!r.Contains(Rect(10, 19, 5, 5))); + EXPECT_TRUE(!r.Contains(Rect(10, 20, 6, 5))); + EXPECT_TRUE(!r.Contains(Rect(10, 20, 5, 6))); } {