Browse Source

Function for angle between vectors.

vectorfields
Vladimír Vondruš 16 years ago
parent
commit
75358451ea
  1. 8
      src/Math/Test/VectorTest.cpp
  2. 1
      src/Math/Test/VectorTest.h
  3. 5
      src/Math/Vector.h

8
src/Math/Test/VectorTest.cpp

@ -26,6 +26,7 @@ using namespace std;
namespace Magnum { namespace Math { namespace Test {
typedef Vector<float, 4> Vector4;
typedef Vector<float, 3> 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);
}
}}}

1
src/Math/Test/VectorTest.h

@ -32,6 +32,7 @@ class VectorTest: public QObject {
void addSubstract();
void length();
void normalized();
void angle();
};
}}}

5
src/Math/Vector.h

@ -29,6 +29,11 @@ namespace Magnum { namespace Math {
/** @brief Vector */
template<class T, size_t size> class Vector {
public:
/** @brief Angle between vectors */
inline static T angle(const Vector<T, size>& a, const Vector<T, size>& b) {
return acos((a*b)/(a.length()*b.length()));
}
/** @brief Default constructor */
inline Vector() {
memset(_data, 0, size*sizeof(T));

Loading…
Cancel
Save