From 75358451eacf530f25e4320c5a8a23a20c176cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 27 Dec 2010 18:38:16 +0100 Subject: [PATCH] Function for angle between vectors. --- src/Math/Test/VectorTest.cpp | 8 ++++++++ src/Math/Test/VectorTest.h | 1 + src/Math/Vector.h | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index c4f0ebe88..a60af9923 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -26,6 +26,7 @@ using namespace std; namespace Magnum { namespace Math { namespace Test { typedef Vector Vector4; +typedef Vector Vector3; void VectorTest::construct() { float zero[] = { 0.0f, 0.0f, 0.0f, 0.0f }; @@ -117,4 +118,11 @@ void VectorTest::normalized() { QVERIFY(Vector4(vec).normalized() == Vector4(normalized)); } +void VectorTest::angle() { + float a[] = { 2.0f, 3.0f, 4.0f }; + float b[] = { 1.0f, -2.0f, 3.0f }; + + QCOMPARE(Vector3::angle(a, b), 1.16251f); +} + }}} diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index e2d42a422..83a2917d3 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -32,6 +32,7 @@ class VectorTest: public QObject { void addSubstract(); void length(); void normalized(); + void angle(); }; }}} diff --git a/src/Math/Vector.h b/src/Math/Vector.h index ffeac7f40..e7ce856e1 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -29,6 +29,11 @@ namespace Magnum { namespace Math { /** @brief Vector */ template class Vector { public: + /** @brief Angle between vectors */ + inline static T angle(const Vector& a, const Vector& b) { + return acos((a*b)/(a.length()*b.length())); + } + /** @brief Default constructor */ inline Vector() { memset(_data, 0, size*sizeof(T));