diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index d436a22..abc6db7 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -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 /// diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index bf30654..466b9aa 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -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);