1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// Calculate the minimum distance between two geometries.
pub trait Distance<F, Origin, Destination> {
/// Note that not all implementations support all geometry combinations, but at least `Point` to `Point`
/// is supported.
/// See [specific implementations](#implementors) for details.
///
/// # Units
///
/// - `origin`, `destination`: geometry where the units of x/y depend on the trait implementation.
/// - returns: depends on the trait implementation.
///
/// # Examples
///
/// ```
/// use geo::{Haversine, Euclidean, Distance, Point};
/// let p1: Point = Point::new(0.0, 0.0);
/// let p2: Point = Point::new(0.0, 2.0);
///
/// assert_eq!(Euclidean.distance(p1, p2), 2.0);
///
/// // The units of the output depend on the metric space.
/// // In the case of [`Haversine`], it's meters.
/// // See the documentation for each metric space for details.
/// assert_eq!(Haversine.distance(p1, p2).round(), 222_390.0);
/// ```
fn distance(&self, origin: Origin, destination: Destination) -> F;
/// Returns `true` if the minimum distance between `origin` and `destination` is less than
/// or equal to `distance`
///
/// # Notes
/// When the calculated distance is 0 (intersecting / touching / overlapping geometries), _any_ positive `distance` value will return `True`
fn distance_within(&self, origin: Origin, destination: Destination, distance: F) -> bool
where
F: PartialOrd,
{
self.distance(origin, destination) <= distance
}
}