Implement Rect::Extend

This commit is contained in:
Dmitry Marakasov 2015-01-19 19:57:40 +03:00
parent 0de09713c9
commit 14f352dddd
3 changed files with 43 additions and 0 deletions

View File

@ -171,6 +171,20 @@ Rect& Rect::Union(const Rect& rect) {
return *this;
}
Rect Rect::GetExtension(unsigned int amount) const {
Rect r = *this;
r.Extend(amount);
return r;
}
Rect& Rect::Extend(unsigned int amount) {
x -= amount;
y -= amount;
w += amount * 2;
h += amount * 2;
return *this;
}
Optional<Rect> Rect::GetIntersection(const Rect& rect) const {
if (!Intersects(rect))
return NullOpt;

View File

@ -346,6 +346,26 @@ public:
////////////////////////////////////////////////////////////
Rect& Union(const Rect& rect);
////////////////////////////////////////////////////////////
/// \brief Get a rect extended by specified amount of pixels
///
/// \param[in] int Number of pixels to extend by
///
/// \returns Reference to self
///
////////////////////////////////////////////////////////////
Rect GetExtension(unsigned int amount) const;
////////////////////////////////////////////////////////////
/// \brief Extend a rect by specified amount of pixels
///
/// \param[in] int Number of pixels to extend by
///
/// \returns Extended rect
///
////////////////////////////////////////////////////////////
Rect& Extend(unsigned int amount);
////////////////////////////////////////////////////////////
/// \brief Calculate intersection with another rect
///

View File

@ -240,6 +240,15 @@ BEGIN_TEST()
EXPECT_TRUE(Rect(30, 40, 1, 1).Union(Rect(10, 20, 1, 1)) == Rect::FromCorners(10, 20, 30, 40));
}
{
// Rect extend
EXPECT_TRUE(Rect(10, 20, 30, 40).GetExtension(0) == Rect(10, 20, 30, 40));
EXPECT_TRUE(Rect(10, 20, 30, 40).GetExtension(10) == Rect(0, 10, 50, 60));
EXPECT_TRUE(Rect(10, 20, 30, 40).Extend(0) == Rect(10, 20, 30, 40));
EXPECT_TRUE(Rect(10, 20, 30, 40).Extend(10) == Rect(0, 10, 50, 60));
}
{
// Rect offset
Rect r(1, 2, 3, 4);