Browse Source

Math: function for projecting vector onto another.

pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
5de5b34c48
  1. 9
      src/Math/Test/VectorTest.cpp
  2. 1
      src/Math/Test/VectorTest.h
  3. 9
      src/Math/Vector.h

9
src/Math/Test/VectorTest.cpp

@ -38,6 +38,7 @@ VectorTest::VectorTest() {
&VectorTest::dotSelf,
&VectorTest::length,
&VectorTest::normalized,
&VectorTest::projected,
&VectorTest::sum,
&VectorTest::product,
&VectorTest::min,
@ -93,6 +94,14 @@ void VectorTest::normalized() {
CORRADE_COMPARE(Vector4(1.0f, 1.0f, 1.0f, 1.0f).normalized(), Vector4(0.5f, 0.5f, 0.5f, 0.5f));
}
void VectorTest::projected() {
Vector3 line(1.0f, -1.0f, 0.5f);
Vector3 projected = Vector3(1.0f, 2.0f, 3.0f).projected(line);
CORRADE_COMPARE(projected, Vector3(0.222222f, -0.222222f, 0.111111f));
CORRADE_COMPARE(projected.normalized(), line.normalized());
}
void VectorTest::sum() {
CORRADE_COMPARE(Vector3(1.0f, 2.0f, 4.0f).sum(), 7.0f);
}

1
src/Math/Test/VectorTest.h

@ -30,6 +30,7 @@ class VectorTest: public Corrade::TestSuite::Tester {
void dotSelf();
void length();
void normalized();
void projected();
void sum();
void product();
void min();

9
src/Math/Vector.h

@ -224,6 +224,15 @@ template<std::size_t size, class T> class Vector: public RectangularMatrix<1, si
return *this/length();
}
/**
* @brief Vector projected onto another
*
* Returns vector projected onto line defined by @p other.
*/
inline Vector<size, T> projected(const Vector<size, T>& other) const {
return other*dot(*this, other)/other.dot();
}
/** @brief Sum of values in the vector */
T sum() const {
T out(0);

Loading…
Cancel
Save