From fd7c0829b69778305fa9a4f6b7074f05162f967c Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Fri, 19 Feb 2016 23:59:01 +0300 Subject: [PATCH] Add more variants of Rect constructors In practice, it's sometimes needed to construct rect from given coordinates + size or two corner coordinates where one of these arguments is a Point and another is a pair of ints. Provide corresponding constructors. --- SDL2pp/Rect.hh | 46 +++++++++++++++++++++++++++++++++++++++++ tests/test_pointrect.cc | 12 +++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index 1731ccc..3a2a786 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -88,6 +88,28 @@ public: constexpr Rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} { } + //////////////////////////////////////////////////////////// + /// \brief Construct the rect from given corner coordinates, width and height + /// + /// \param[in] corner Coordinates of the top left rectangle corner + /// \param[in] w Width of the rectangle + /// \param[in] h Height of the rectangle + /// + //////////////////////////////////////////////////////////// + constexpr Rect(const Point& corner, int w, int h) : SDL_Rect{corner.x, corner.y, w, h} { + } + + //////////////////////////////////////////////////////////// + /// \brief Construct the rect from given corner coordinates, width and height + /// + /// \param[in] x X coordinate of the top left rectangle corner + /// \param[in] y Y coordinate of the top left rectangle corner + /// \param[in] size Dimensions of the rectangle + /// + //////////////////////////////////////////////////////////// + constexpr Rect(int x, int y, const Point& size) : SDL_Rect{x, y, size.x, size.y} { + } + //////////////////////////////////////////////////////////// /// \brief Construct the rect from given center coordinates, width and height /// @@ -136,6 +158,30 @@ public: return Rect(p1, p2 - p1 + Point(1, 1)); } + //////////////////////////////////////////////////////////// + /// \brief Construct the rect from given corners coordinates + /// + /// \param[in] p1 Coordinates of the top left rectangle corner + /// \param[in] x2 X coordinate of the bottom right rectangle corner + /// \param[in] y2 Y coordinate of the bottom right rectangle corner + /// + //////////////////////////////////////////////////////////// + static constexpr Rect FromCorners(const Point& p1, int x2, int y2) { + return Rect(p1, Point(x2, y2) - p1 + Point(1, 1)); + } + + //////////////////////////////////////////////////////////// + /// \brief Construct the rect from given corners coordinates + /// + /// \param[in] x1 X coordinate of the top left rectangle corner + /// \param[in] y1 Y coordinate of the top left rectangle corner + /// \param[in] p2 Coordinates of the bottom right rectangle corner + /// + //////////////////////////////////////////////////////////// + static constexpr Rect FromCorners(int x1, int y1, const Point& p2) { + return Rect(x1, y1, p2 - Point(x1, y1) + Point(1, 1)); + } + //////////////////////////////////////////////////////////// /// \brief Copy constructor /// diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 654cde0..f53fff0 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -40,7 +40,8 @@ BEGIN_TEST(int, char*[]) Point p(8,9); p = Point(10,11); - p = p; + Point& pref = p; + p = pref; EXPECT_TRUE(p.GetX() == 10 && p.GetY() == 11); } @@ -135,7 +136,8 @@ BEGIN_TEST(int, char*[]) Rect r(13,14,15,16); r = Rect(17,18,19,20); - r = r; + Rect& rref = r; + r = rref; EXPECT_TRUE(r.GetX() == 17 && r.GetY() == 18 && r.GetW() == 19 && r.GetH() == 20); } @@ -165,10 +167,16 @@ BEGIN_TEST(int, char*[]) { // Constructors + EXPECT_EQUAL(Rect(10, 20, 30, 40), Rect(10, 20, Point(30, 40))); + EXPECT_EQUAL(Rect(10, 20, 30, 40), Rect(Point(10, 20), 30, 40)); + EXPECT_EQUAL(Rect(10, 20, 30, 40), Rect(Point(10, 20), Point(30, 40))); + EXPECT_EQUAL(Rect::FromCenter(100, 100, 5, 7), Rect(98, 97, 5, 7)); EXPECT_EQUAL(Rect::FromCenter(Point(100, 100), Point(5, 7)), Rect(98, 97, 5, 7)); EXPECT_EQUAL(Rect::FromCorners(10, 20, 30, 40), Rect(10, 20, 21, 21)); + EXPECT_EQUAL(Rect::FromCorners(Point(10, 20), 30, 40), Rect(10, 20, 21, 21)); + EXPECT_EQUAL(Rect::FromCorners(10, 20, Point(30, 40)), Rect(10, 20, 21, 21)); EXPECT_EQUAL(Rect::FromCorners(Point(10, 20), Point(30, 40)), Rect(10, 20, 21, 21)); }