Implement Rect::Contains(Rect), add corresponding test

This commit is contained in:
Dmitry Marakasov 2014-12-29 22:04:43 +03:00
parent 518f9fff35
commit 7506b05a77
3 changed files with 23 additions and 0 deletions

View File

@ -129,6 +129,10 @@ bool Rect::Contains(const Point& point) const {
return !(point.x < x || point.y < y || point.x > GetX2() || point.y > GetY2()); 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 { Rect Rect::operator+(const Point& offset) const {
return Rect(x + offset.x, y + offset.y, w, h); return Rect(x + offset.x, y + offset.y, w, h);
} }

View File

@ -253,6 +253,16 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool Contains(const Point& point) const; 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 /// \brief Get rectangle moved by a given offset
/// ///

View File

@ -155,6 +155,15 @@ BEGIN_TEST()
EXPECT_TRUE(!r.Contains(Point(10, 19))); EXPECT_TRUE(!r.Contains(Point(10, 19)));
EXPECT_TRUE(!r.Contains(Point(15, 20))); EXPECT_TRUE(!r.Contains(Point(15, 20)));
EXPECT_TRUE(!r.Contains(Point(10, 25))); 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)));
} }
{ {