distance v0.2.2 Distance

Basic distance calculations for cartesian coordinates for calculting distances on a single plane. If you are looking to calculating distance on the surface of Earth, check out the Distance.GreatCircle module.

Examples

iex> Distance.distance({2.5, 2.5}, {4, 0.8})
2.2671568097509267
iex> Distance.segment_distance({2.5, 2.5}, {4, 0.8}, {-2, 3})
1.0797077632696
iex> Distance.distance([{2.5, 2.5}, {4, 0.8}, {-2, 3}, {1, -1}])
13.657774933219109

Summary

Functions

Returns the geometric distance of the linestring defined by the List of points. Accepts 2- or 3-dimensional points

Returns the geometric distance between two points. Accepts 2- or 3-dimensional points

Returns the square of the distance between two points. This is used by the Distance.distance function above, but having access to the value before the expensice sqaure root operation is useful for time-sensitive applications that only need values for comparison

Returns the geometric distance from a point p and the line segment between two points p1 and p2. Note that this is a line segment, not an infinite line, so points not between p1 and p2 will return the distance to the nearest of the two endpoints

Similar to Distance.distance_squared, this provides much faster comparable version of Distance.segment_distance

Functions

distance(list)

Returns the geometric distance of the linestring defined by the List of points. Accepts 2- or 3-dimensional points.

Examples

iex> Distance.distance([{2.5, 2.5}, {4, 0.8}, {2.5, 3.1}, {2.5, 3.1}])
5.013062853300123
iex> Distance.distance([{1, -2, 1}, {-2, 2, -1}, {-2, 1, 0}, {2, -3, 1}])
12.543941016045627
distance(p1, p2)

Returns the geometric distance between two points. Accepts 2- or 3-dimensional points.

Examples

iex> Distance.distance({1, -2}, {-2, 2})
5.0
iex> Distance.distance({1, -2, 2}, {-2, 2, 1})
5.0990195135927845
distance_squared(arg1, arg2)

Returns the square of the distance between two points. This is used by the Distance.distance function above, but having access to the value before the expensice sqaure root operation is useful for time-sensitive applications that only need values for comparison.

Examples

iex> Distance.distance_squared({1, -2}, {-2, 2})
25
iex> Distance.distance_squared({1, -2, 2}, {-2, 2, 1})
26
segment_distance(p, p1, p2)

Returns the geometric distance from a point p and the line segment between two points p1 and p2. Note that this is a line segment, not an infinite line, so points not between p1 and p2 will return the distance to the nearest of the two endpoints.

Examples

iex> Distance.segment_distance({3, 2}, {-2, 1}, {5, 3})
0.4120816918460673 # distance between the point {3, 2} and the closest point along line segment ({-2, 1}, {5, 3})
iex> Distance.segment_distance({1, -2}, {-2, 2}, {-10, 102})
5.0
iex> Distance.segment_distance({1, -2}, {-2, 2}, {1, -2})
0.0
segment_distance_squared(arg1, arg2, arg3)

Similar to Distance.distance_squared, this provides much faster comparable version of Distance.segment_distance.

Examples

iex> Distance.segment_distance_squared({3, 2}, {-2, 1}, {5, 3})
0.16981132075471717
iex> Distance.segment_distance_squared({1, -2}, {-2, 2}, {-10, 102})
25