From 32839006f33f988daf86cadc4de715233a9dc858 Mon Sep 17 00:00:00 2001 From: jgoppert Date: Thu, 5 Nov 2015 20:22:17 -0500 Subject: [PATCH] Implemented vector 3 cross product. --- matrix/Vector3.hpp | 12 +++++------- test/vector3.cpp | 2 ++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/matrix/Vector3.hpp b/matrix/Vector3.hpp index ad852a88d9..0629099088 100644 --- a/matrix/Vector3.hpp +++ b/matrix/Vector3.hpp @@ -50,14 +50,12 @@ public: v(2) = z; } - Vector3 cross(const Vector3 & b) { - // TODO - Vector3 &a(*this); - (void)a; + Vector3 cross(const Vector3 & b) const { + const Vector3 &a(*this); Vector3 c; - c(0) = 0; - c(1) = 0; - c(2) = 0; + c(0) = a(1)*b(2) - a(2)*b(1); + c(1) = -a(0)*b(2) + a(2)*b(0); + c(2) = a(0)*b(1) - a(1)*b(0); return c; } }; diff --git a/test/vector3.cpp b/test/vector3.cpp index a7a033bf38..e60a3f201f 100644 --- a/test/vector3.cpp +++ b/test/vector3.cpp @@ -12,6 +12,8 @@ int main() Vector3f a(1, 0, 0); Vector3f b(0, 1, 0); Vector3f c = a.cross(b); + c.print(); + assert (c == Vector3f(0,0,1)); return 0; }