From 66e6ab6684ee5b3cdd748fa335a014a3b0f4aaec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 28 Nov 2013 20:27:07 +0100 Subject: [PATCH] Math: added Range::padded() function. --- src/Math/Range.h | 17 +++++++++++++++++ src/Math/Test/RangeTest.cpp | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Math/Range.h b/src/Math/Range.h index eff56d0a6..2df1c2c07 100644 --- a/src/Math/Range.h +++ b/src/Math/Range.h @@ -143,9 +143,19 @@ template class Range { * * Translates the minimal and maximal coordinates by given amount. Size * remains the same. + * @see @ref padded() */ Range translated(const VectorType& vector) const; + /** + * @brief Padded rage + * + * Translates the minimal and maximal coordinates by given amount. + * Center remains the same. + * @see @ref translated() + */ + Range padded(const VectorType& padding) const; + private: VectorType _min, _max; }; @@ -157,6 +167,9 @@ template class Range { } \ Type translated(const VectorType& vector) const { \ return Range::translated(vector); \ + } \ + Type padded(const VectorType& padding) const { \ + return Range::padded(padding); \ } #endif @@ -437,6 +450,10 @@ template Range Range Range Range::padded(const VectorType& padding) const { + return {_min - padding, _max + padding}; +} + }} namespace Corrade { namespace Utility { diff --git a/src/Math/Test/RangeTest.cpp b/src/Math/Test/RangeTest.cpp index a6a482071..c3b6e660c 100644 --- a/src/Math/Test/RangeTest.cpp +++ b/src/Math/Test/RangeTest.cpp @@ -50,6 +50,7 @@ class RangeTest: public Corrade::TestSuite::Tester { void center(); void translated(); + void padded(); void subclassTypes(); void subclass(); @@ -80,6 +81,7 @@ RangeTest::RangeTest() { &RangeTest::center, &RangeTest::translated, + &RangeTest::padded, &RangeTest::subclassTypes, &RangeTest::subclass, @@ -282,6 +284,14 @@ void RangeTest::translated() { CORRADE_COMPARE(a.size(), b.size()); } +void RangeTest::padded() { + Range2Di a({34, 23}, {47, 30}); + Range2Di b({31, 28}, {50, 25}); + + CORRADE_COMPARE(a.padded({3, -5}), b); + CORRADE_COMPARE(a.center(), b.center()); +} + template class BasicRect: public Math::Range<2, T> { public: template BasicRect(U&&... args): Math::Range<2, T>{std::forward(args)...} {} @@ -297,6 +307,7 @@ void RangeTest::subclassTypes() { const Recti r; CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); } void RangeTest::subclass() { @@ -305,6 +316,8 @@ void RangeTest::subclass() { CORRADE_COMPARE(Recti(Vector2i{34, 23}, Vector2i{47, 30}).translated({-17, 40}), Recti(Vector2i{17, 63}, Vector2i{30, 70})); + CORRADE_COMPARE(Recti(Vector2i{34, 23}, Vector2i{47, 30}).padded({3, -5}), + Recti(Vector2i{31, 28}, Vector2i{50, 25})); } void RangeTest::debug() {