From 4e2f56c78b5b79b1e206f5b8ee8767f3ce2c1226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 27 Dec 2010 18:44:40 +0100 Subject: [PATCH] Function for negating a vector. --- src/Math/Test/VectorTest.cpp | 7 +++++++ src/Math/Test/VectorTest.h | 1 + src/Math/Vector.h | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index a60af9923..7d4f23d15 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -125,4 +125,11 @@ void VectorTest::angle() { QCOMPARE(Vector3::angle(a, b), 1.16251f); } +void VectorTest::negative() { + float vec[] = { 1.0f, -3.0f, 5.0f, -10.0f }; + float negative[] = { -1.0f, 3.0f, -5.0f, 10.0f }; + + QVERIFY(-Vector4(vec) == negative); +} + }}} diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index 83a2917d3..05dfe2e7b 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -33,6 +33,7 @@ class VectorTest: public QObject { void length(); void normalized(); void angle(); + void negative(); }; }}} diff --git a/src/Math/Vector.h b/src/Math/Vector.h index e7ce856e1..e7c709b10 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -148,6 +148,16 @@ template class Vector { return out; } + /** @brief Negative vector */ + Vector operator-() const { + Vector out; + + for(size_t i = 0; i != size; ++i) + out.set(i, -at(i)); + + return out; + } + /** @brief Vector length */ T length() const { T out(0);