diff --git a/src/Math/Geometry/Distance.h b/src/Math/Geometry/Distance.h index f4868a601..6816df743 100644 --- a/src/Math/Geometry/Distance.h +++ b/src/Math/Geometry/Distance.h @@ -29,7 +29,6 @@ */ #include "Math/Functions.h" -#include "Math/Matrix.h" #include "Math/Vector3.h" namespace Magnum { namespace Math { namespace Geometry { @@ -46,14 +45,15 @@ class Distance { * @param point Point * * The distance *d* is computed from point **p** and line defined by **a** - * and **b** using @ref Matrix::determinant() "determinant": @f[ - * d = \frac{|det(b - a a - point)|} {|b - a|} + * and **b** using @ref Vector2::cross() "perp-dot product": @f[ + * d = \frac{|(\boldsymbol b - \boldsymbol a)_\bot \cdot (\boldsymbol a - \boldsymbol p)|} {|\boldsymbol b - \boldsymbol a|} * @f] * Source: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html * @see linePointSquared(const Vector2&, const Vector2&, const Vector2&) */ template inline static T linePoint(const Vector2& a, const Vector2& b, const Vector2& point) { - return std::abs(Matrix<2, T>(b - a, a - point).determinant())/(b - a).length(); + const Vector2 bMinusA = b - a; + return std::abs(Vector2::cross(bMinusA, a - point))/bMinusA.length(); } /** @@ -67,8 +67,8 @@ class Distance { * compute the square root. */ template inline static T linePointSquared(const Vector2& a, const Vector2& b, const Vector2& point) { - Vector2 bMinusA = b - a; - return Math::pow<2>(Matrix<2, T>(bMinusA, a - point).determinant())/bMinusA.dot(); + const Vector2 bMinusA = b - a; + return Math::pow<2>(Vector2::cross(bMinusA, a - point))/bMinusA.dot(); } /** @@ -127,12 +127,12 @@ class Distance { * @see lineSegmentPointSquared() */ template inline static T lineSegmentPoint(const Vector2& a, const Vector2& b, const Vector2& point) { - Vector2 pointMinusA = point - a; - Vector2 pointMinusB = point - b; - Vector2 bMinusA = b - a; - T pointDistanceA = pointMinusA.dot(); - T pointDistanceB = pointMinusB.dot(); - T bDistanceA = bMinusA.dot(); + const Vector2 pointMinusA = point - a; + const Vector2 pointMinusB = point - b; + const Vector2 bMinusA = b - a; + const T pointDistanceA = pointMinusA.dot(); + const T pointDistanceB = pointMinusB.dot(); + const T bDistanceA = bMinusA.dot(); /* Point is before A */ if(pointDistanceB > bDistanceA + pointDistanceA) @@ -143,7 +143,7 @@ class Distance { return std::sqrt(pointDistanceB); /* Between A and B */ - return std::abs(Matrix<2, T>(bMinusA, -pointMinusA).determinant())/std::sqrt(bDistanceA); + return std::abs(Vector2::cross(bMinusA, -pointMinusA))/std::sqrt(bDistanceA); } /** @@ -153,12 +153,12 @@ class Distance { * other values, because it doesn't compute the square root. */ template static T lineSegmentPointSquared(const Vector2& a, const Vector2& b, const Vector2& point) { - Vector2 pointMinusA = point - a; - Vector2 pointMinusB = point - b; - Vector2 bMinusA = b - a; - T pointDistanceA = pointMinusA.dot(); - T pointDistanceB = pointMinusB.dot(); - T bDistanceA = bMinusA.dot(); + const Vector2 pointMinusA = point - a; + const Vector2 pointMinusB = point - b; + const Vector2 bMinusA = b - a; + const T pointDistanceA = pointMinusA.dot(); + const T pointDistanceB = pointMinusB.dot(); + const T bDistanceA = bMinusA.dot(); /* Point is before A */ if(pointDistanceB > bDistanceA + pointDistanceA) @@ -169,7 +169,7 @@ class Distance { return pointDistanceB; /* Between A and B */ - return Math::pow<2>(Matrix<2, T>(bMinusA, -pointMinusA).determinant())/bDistanceA; + return Math::pow<2>(Vector2::cross(bMinusA, -pointMinusA))/bDistanceA; } /** @@ -194,11 +194,11 @@ class Distance { * other values, because it doesn't compute the square root. */ template static T lineSegmentPointSquared(const Vector3& a, const Vector3& b, const Vector3& point) { - Vector3 pointMinusA = point - a; - Vector3 pointMinusB = point - b; - T pointDistanceA = pointMinusA.dot(); - T pointDistanceB = pointMinusB.dot(); - T bDistanceA = (b - a).dot(); + const Vector3 pointMinusA = point - a; + const Vector3 pointMinusB = point - b; + const T pointDistanceA = pointMinusA.dot(); + const T pointDistanceB = pointMinusB.dot(); + const T bDistanceA = (b - a).dot(); /* Point is before A */ if(pointDistanceB > bDistanceA + pointDistanceA)