# Magnum::Math::Distance namespace#include <Magnum/Math/Distance.h>

Functions for calculating distances.

### Contents

• Reference

This library is built as part of Magnum by default. To use this library with CMake, find the Magnum package and link to the Magnum::Magnum target:

find_package(Magnum REQUIRED)

# ...
target_link_libraries(your-app PRIVATE Magnum::Magnum)

## Functions

template<class T>
auto linePointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) -> T
Distance of line and point in 2D, squared.
template<class T>
auto linePoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) -> T
Distance of line and point in 2D.
template<class T>
auto linePointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) -> T
Distance of line and point in 3D, squared.
template<class T>
auto linePoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) -> T
Distance of line and point in 3D.
template<class T>
const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) -> T
Distance of point from line segment in 2D, squared.
template<class T>
auto lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point) -> T
Dístance of point from line segment in 2D.
template<class T>
const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) -> T
Distance of point from line segment in 3D, squared.
template<class T>
auto lineSegmentPoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point) -> T
Dístance of point from line segment in 3D.
template<class T>
auto pointPlaneScaled(const Vector3<T>& point, const Vector4<T>& plane) -> T
Distance of point from plane, scaled by the length of the planes normal.
template<class T>
auto pointPlane(const Vector3<T>& point, const Vector4<T>& plane) -> T
Distance of point from plane.
template<class T>
auto pointPlaneNormalized(const Vector3<T>& point, const Vector4<T>& plane) -> T
Distance of point from plane with normalized normal.

## Function documentation

### template<class T> T Magnum::Math::Distance::linePointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point)

Distance of line and point in 2D, squared.

Parameters
a First point of the line
b Second point of the line
point Point

More efficient than linePoint(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&) for comparing distance with other values, because it doesn't calculate the square root.

### template<class T> T Magnum::Math::Distance::linePoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point)

Distance of line and point in 2D.

Parameters
a First point of the line
b Second point of the line
point Point

The distance is calculated from point and line defined by and using perp-dot product:

### template<class T> T Magnum::Math::Distance::linePointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point)

Distance of line and point in 3D, squared.

More efficient than linePoint(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&) for comparing distance with other values, because it doesn't calculate the square root.

### template<class T> T Magnum::Math::Distance::linePoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point)

Distance of line and point in 3D.

Parameters
a First point of the line
b Second point of the line
point Point

The distance is calculated from point and line defined by and using cross product:

### template<class T> T Magnum::Math::Distance::lineSegmentPointSquared(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point)

Distance of point from line segment in 2D, squared.

More efficient than lineSegmentPoint() for comparing distance with other values, because it doesn't calculate the square root.

### template<class T> T Magnum::Math::Distance::lineSegmentPoint(const Vector2<T>& a, const Vector2<T>& b, const Vector2<T>& point)

Dístance of point from line segment in 2D.

Parameters
a Starting point of the line
b Ending point of the line
point Point

Returns distance of point from line segment or from its starting/ending point, depending on where the point lies.

Determining whether the point lies next to line segment or outside is done using Pythagorean theorem. If the following equation applies, the point lies outside line segment closer to :

On the other hand, if the following equation applies, the point lies outside line segment closer to :

The last alternative is when the following equation applies. The point then lies between and and the distance is calculated the same way as in linePoint().

### template<class T> T Magnum::Math::Distance::lineSegmentPointSquared(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point)

Distance of point from line segment in 3D, squared.

More efficient than lineSegmentPoint(const Vector3<T>&, const Vector3<T>&, const Vector3<T>&) for comparing distance with other values, because it doesn't calculate the square root.

### template<class T> T Magnum::Math::Distance::lineSegmentPoint(const Vector3<T>& a, const Vector3<T>& b, const Vector3<T>& point)

Dístance of point from line segment in 3D.

Parameters
a Starting point of the line
b Ending point of the line
point Point

Similar to 2D implementation lineSegmentPoint(const Vector2<T>&, const Vector2<T>&, const Vector2<T>&).

### template<class T> T Magnum::Math::Distance::pointPlaneScaled(const Vector3<T>& point, const Vector4<T>& plane)

Distance of point from plane, scaled by the length of the planes normal.

The distance is calculated from point and plane with normal and using:

The distance is negative if the point lies behind the plane.

More efficient than pointPlane() when merely the sign of the distance is of interest, for example when testing on which half space of the plane the point lies.

### template<class T> T Magnum::Math::Distance::pointPlane(const Vector3<T>& point, const Vector4<T>& plane)

Distance of point from plane.

The distance is calculated from point and plane with normal and using:

The distance is negative if the point lies behind the plane.

In cases where the planes normal is a unit vector, pointPlaneNormalized() is more efficient. If merely the sign of the distance is of interest, pointPlaneScaled() is more efficient.

### template<class T> T Magnum::Math::Distance::pointPlaneNormalized(const Vector3<T>& point, const Vector4<T>& plane)

Distance of point from plane with normalized normal.

The distance is calculated from point and plane with normal and using:

The distance is negative if the point lies behind the plane. Expects that plane normal is normalized.

More efficient than pointPlane() in cases where the plane's normal is normalized. Equivalent to pointPlaneScaled() but with assertion added on top.