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.
This commit is contained in:
Dmitry Marakasov 2016-02-19 23:59:01 +03:00
parent e93158e9e6
commit fd7c0829b6
2 changed files with 56 additions and 2 deletions

View File

@ -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
///

View File

@ -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));
}