From 3213d5b7a5c8670d04aa71d028107859d728dd6c Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 15 May 2011 11:16:18 +0000 Subject: [PATCH] core: add the unary minus operator to vectors. --- src/matrix.h | 8 ++++++++ test/matrix.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/matrix.h b/src/matrix.h index f79fd14a..89d9fa7d 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -103,6 +103,14 @@ namespace lol return ret; \ } \ \ + inline Vec##elems operator -() const \ + { \ + Vec##elems ret; \ + for (int n = 0; n < elems; n++) \ + ret[n] = -(*this)[n]; \ + return ret; \ + } \ + \ inline T sqlen() const \ { \ T acc = 0; \ diff --git a/test/matrix.cpp b/test/matrix.cpp index 3a8ef2b4..cca9ada3 100644 --- a/test/matrix.cpp +++ b/test/matrix.cpp @@ -27,6 +27,7 @@ class MatrixTest : public CppUnit::TestCase CPPUNIT_TEST_SUITE(MatrixTest); CPPUNIT_TEST(test_vec_eq); CPPUNIT_TEST(test_vec_lt); + CPPUNIT_TEST(test_vec_unary); CPPUNIT_TEST(test_mat_det); CPPUNIT_TEST(test_mat_mul); CPPUNIT_TEST(test_mat_inv); @@ -122,6 +123,15 @@ public: CPPUNIT_ASSERT(a2 < f2); } + void test_vec_unary() + { + vec2 a(1.0f, 3.0f); + vec2 b(-1.0f, -3.0f); + + CPPUNIT_ASSERT(a == -b); + CPPUNIT_ASSERT(-a == b); + } + void test_mat_det() { float d1 = triangular.det();