Add Rect point getters

This commit is contained in:
Dmitry Marakasov 2015-11-27 20:21:49 +03:00
parent 693cdc659f
commit 8003052f32
2 changed files with 70 additions and 0 deletions

View File

@ -306,6 +306,66 @@ public:
return *this;
}
////////////////////////////////////////////////////////////
/// \brief Get top left corner of the rect
///
/// \returns Top left corner of the rect
///
////////////////////////////////////////////////////////////
constexpr Point GetTopLeft() const {
return Point(x, y);
}
////////////////////////////////////////////////////////////
/// \brief Get top right corner of the rect
///
/// \returns Top right corner of the rect
///
////////////////////////////////////////////////////////////
constexpr Point GetTopRight() const {
return Point(GetX2(), y);
}
////////////////////////////////////////////////////////////
/// \brief Get bottom left corner of the rect
///
/// \returns bottom left corner of the rect
///
////////////////////////////////////////////////////////////
constexpr Point GetBottomLeft() const {
return Point(x, GetY2());
}
////////////////////////////////////////////////////////////
/// \brief Get bottom right corner of the rect
///
/// \returns Bottom right corner of the rect
///
////////////////////////////////////////////////////////////
constexpr Point GetBottomRight() const {
return Point(GetX2(), GetY2());
}
////////////////////////////////////////////////////////////
/// \brief Get size of the rect
///
/// \returns Size of the rect
///
////////////////////////////////////////////////////////////
constexpr Point GetSize() const {
return Point(w, h);
}
////////////////////////////////////////////////////////////
/// \brief Get centroid of the rect
///
/// \returns Centroid of the rect
///
////////////////////////////////////////////////////////////
constexpr Point GetCentroid() const {
return Point(x + w/2, y + h/2);
}
////////////////////////////////////////////////////////////
/// \brief Check whether the rect contains given point
///

View File

@ -290,6 +290,16 @@ BEGIN_TEST()
EXPECT_EQUAL(Rect(10, 20, 30, 40).Extend(10, 20), Rect(0, 0, 50, 80));
}
{
// Rect point getters
EXPECT_EQUAL(Rect(10, 20, 30, 40).GetTopLeft(), Point(10, 20));
EXPECT_EQUAL(Rect(10, 20, 30, 40).GetTopRight(), Point(39, 20));
EXPECT_EQUAL(Rect(10, 20, 30, 40).GetBottomLeft(), Point(10, 59));
EXPECT_EQUAL(Rect(10, 20, 30, 40).GetBottomRight(), Point(39, 59));
EXPECT_EQUAL(Rect(10, 20, 30, 40).GetSize(), Point(30, 40));
EXPECT_EQUAL(Rect(10, 20, 30, 40).GetCentroid(), Point(25, 40));
}
{
// Rect offset
Rect r(1, 2, 3, 4);