diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index af16f76..d04f598 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -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::GetIntersection(const Rect& rect) const { if (!Intersects(rect)) return NullOpt; diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index e45eb0d..79f7139 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -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 /// diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 780baf0..bb38842 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -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);