From 06872c77274a54f99adfbf5e5773a7b038f6213e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 6 Aug 2012 17:26:38 +0200 Subject: [PATCH] Added Vector::min() and Vector::max(). --- src/Math/Test/VectorTest.cpp | 10 ++++++++++ src/Math/Test/VectorTest.h | 2 ++ src/Math/Vector.h | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 109f50ca9..327c2cb49 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -42,6 +42,8 @@ VectorTest::VectorTest() { &VectorTest::normalized, &VectorTest::sum, &VectorTest::product, + &VectorTest::min, + &VectorTest::max, &VectorTest::angle, &VectorTest::negative, &VectorTest::debug); @@ -130,6 +132,14 @@ void VectorTest::product() { CORRADE_COMPARE(Vector3(1.0f, 2.0f, 3.0f).product(), 6.0f); } +void VectorTest::min() { + CORRADE_COMPARE(Vector3(1.0f, -2.0f, 3.0f).min(), -2.0f); +} + +void VectorTest::max() { + CORRADE_COMPARE(Vector3(1.0f, -2.0f, 3.0f).max(), 3.0f); +} + void VectorTest::angle() { ostringstream o; Error::setOutput(&o); diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index e00481d59..662865119 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -34,6 +34,8 @@ class VectorTest: public Corrade::TestSuite::Tester { void normalized(); void sum(); void product(); + void min(); + void max(); void angle(); void negative(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 622ee3e00..96b0fe502 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -294,6 +294,26 @@ template class Vector { return out; } + /** @brief Minimal value in the vector */ + T min() const { + T out((*this)[0]); + + for(size_t i = 1; i != size; ++i) + out = std::min(out, (*this)[i]); + + return out; + } + + /** @brief Maximal value in the vector */ + T max() const { + T out((*this)[0]); + + for(size_t i = 1; i != size; ++i) + out = std::max(out, (*this)[i]); + + return out; + } + private: T _data[size]; };