diff --git a/src/Math/Test/Vector2Test.cpp b/src/Math/Test/Vector2Test.cpp index 24ede0935..0d8aa7691 100644 --- a/src/Math/Test/Vector2Test.cpp +++ b/src/Math/Test/Vector2Test.cpp @@ -66,6 +66,7 @@ class Vector2Test: public Corrade::TestSuite::Tester { void cross(); void axes(); void scales(); + void perpendicular(); void debug(); void configuration(); @@ -88,6 +89,7 @@ Vector2Test::Vector2Test() { &Vector2Test::cross, &Vector2Test::axes, &Vector2Test::scales, + &Vector2Test::perpendicular, &Vector2Test::debug, &Vector2Test::configuration}); @@ -187,6 +189,13 @@ void Vector2Test::scales() { CORRADE_COMPARE(y, Vector2(1.0f, -0.2f)); } +void Vector2Test::perpendicular() { + const Vector2 a(0.5f, -15.0f); + CORRADE_COMPARE(a.perpendicular(), Vector2(15.0f, 0.5f)); + CORRADE_COMPARE(Vector2::dot(a.perpendicular(), a), 0.0f); + CORRADE_COMPARE(Vector2::xAxis().perpendicular(), Vector2::yAxis()); +} + void Vector2Test::debug() { std::ostringstream o; Debug(&o) << Vector2(0.5f, 15.0f); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 8d07e9ab5..e49f1700b 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -84,10 +84,12 @@ template class Vector { /** * @brief Dot product * - * @f[ + * Returns `0` if two vectors are orthogonal, `1` if two *normalized* + * vectors are parallel and `-1` if two *normalized* vectors are + * antiparallel. @f[ * \boldsymbol a \cdot \boldsymbol b = \sum_{i=0}^{n-1} \boldsymbol a_i \boldsymbol b_i * @f] - * @see dot() const + * @see dot() const, operator-(), Vector2::perpendicular() */ inline static T dot(const Vector& a, const Vector& b) { return (a*b).sum(); @@ -270,6 +272,7 @@ template class Vector { * The computation is done in-place. @f[ * \boldsymbol a_i = -\boldsymbol a_i * @f] + * @see Vector2::perpendicular() */ Vector operator-() const { Vector out; diff --git a/src/Math/Vector2.h b/src/Math/Vector2.h index b19624d6f..4e7b17c00 100644 --- a/src/Math/Vector2.h +++ b/src/Math/Vector2.h @@ -123,6 +123,16 @@ template class Vector2: public Vector<2, T> { inline T& y() { return (*this)[1]; } /**< @brief Y component */ inline constexpr T y() const { return (*this)[1]; } /**< @overload */ + /** + * @brief Perpendicular vector + * + * Returns vector rotated 90° counterclockwise. @f[ + * \boldsymbol v_\perp = \begin{pmatrix} -v_y \\ v_x \end{pmatrix} + * @f] + * @see dot(const Vector&, const Vector&), operator-() const + */ + inline Vector2 perpendicular() const { return {-y(), x()}; } + MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2) };