logo
pub trait EuclideanDistance<T, Rhs = Self> {
    fn euclidean_distance(&self, rhs: &Rhs) -> T;
}
Expand description

Returns the distance between two geometries.

Required Methods

Returns the distance between two geometries

If a Point is contained by a Polygon, the distance is 0.0

If a Point lies on a Polygon’s exterior or interior rings, the distance is 0.0

If a Point lies on a LineString, the distance is 0.0

The distance between a Point and an empty LineString is 0.0

Examples

Point to Point:

use approx::assert_relative_eq;
use geo::EuclideanDistance;
use geo::point;

let p1 = point!(x: -72.1235, y: 42.3521);
let p2 = point!(x: -72.1260, y: 42.45);

let distance = p1.euclidean_distance(&p2);

assert_relative_eq!(distance, 0.09793191512474639);

Point to Polygon:

use approx::assert_relative_eq;
use geo::EuclideanDistance;
use geo::{point, polygon};

let polygon = polygon![
    (x: 5., y: 1.),
    (x: 4., y: 2.),
    (x: 4., y: 3.),
    (x: 5., y: 4.),
    (x: 6., y: 4.),
    (x: 7., y: 3.),
    (x: 7., y: 2.),
    (x: 6., y: 1.),
    (x: 5., y: 1.),
];

let point = point!(x: 2.5, y: 0.5);

let distance = point.euclidean_distance(&polygon);

assert_relative_eq!(distance, 2.1213203435596424);

Point to LineString:

use approx::assert_relative_eq;
use geo::EuclideanDistance;
use geo::{point, line_string};

let line_string = line_string![
    (x: 5., y: 1.),
    (x: 4., y: 2.),
    (x: 4., y: 3.),
    (x: 5., y: 4.),
    (x: 6., y: 4.),
    (x: 7., y: 3.),
    (x: 7., y: 2.),
    (x: 6., y: 1.),
];

let point = point!(x: 5.5, y: 2.1);

let distance = point.euclidean_distance(&line_string);

assert_relative_eq!(distance, 1.1313708498984762);

Implementors

Line to Line distance

LineString to Line

MultiPolygon to Line distance

Line to LineString

LineString-LineString distance

Polygon to LineString distance

Line to MultiPolygon distance

LineString to Polygon