Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parametric version
Browse files Browse the repository at this point in the history
tmcw committed Aug 21, 2016
1 parent 0be988f commit e26beeb
Showing 2 changed files with 50 additions and 61 deletions.
67 changes: 50 additions & 17 deletions src/algorithm/distance.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use num::Float;
use num::{Float, FromPrimitive};
use types::{Point};

/// Returns the distance between two geometries.
trait Distance<T, Rhs = Self> {
fn distance<A: DistanceAlgorithm>(&self, rhs: &Rhs) -> T;
}

pub trait Distance<T, Rhs = Self>
{
/// Returns the distance between two points:
///
/// ```
/// use geo::{COORD_PRECISION, Point};
/// use geo::algorithm::distance::Distance;
///
/// let p = Point::new(-72.1235, 42.3521);
/// let dist = p.distance(&Point::new(-72.1260, 42.45));
/// assert!(dist < COORD_PRECISION)
/// ```
fn distance(&self, rhs: &Rhs) -> T;
trait EuclideanAlgorithm {
}

impl<T> Distance<T, Point<T>> for Point<T>
where T: Float
{
struct Euclidean;

impl DistanceAlgorithm for Euclidean {
fn distance(&self, p: &Point<T>) -> T {
let (dx, dy) = (self.x() - p.x(), self.y() - p.y());
dx.hypot(dy)
@@ -42,3 +33,45 @@ mod test {
assert!(dist < 147. && dist > 146.);
}
}


/// Returns the distance between two geometries.
// pub trait HaversineDistance<T, Rhs = Self>
// {
// /// Returns the distance between two points:
// ///
// /// ```
// /// use geo::{COORD_PRECISION, Point};
// /// use geo::algorithm::haversine_distance::HaversineDistance;
// ///
// /// let p = Point::new(-72.1235, 42.3521);
// /// let dist = p.haversine_distance(&Point::new(-72.1260, 42.45));
// /// assert_eq!(dist as i32, 10900)
// /// ```
// fn haversine_distance(&self, rhs: &Rhs) -> T;
// }
//
// impl<T> HaversineDistance<T, Point<T>> for Point<T>
// where T: Float + FromPrimitive
// {
// // currently gives answer in meters
// fn haversine_distance(&self, p: &Point<T>) -> T {
// let a = (self.y().to_radians().sin() * p.y().to_radians().sin()) +
// (self.y().to_radians().cos() * p.y().to_radians().cos()) *
// (p.x() - self.x()).to_radians().cos();
// T::from_i32(6378137).unwrap() * a.acos().min(T::one())
// }
// }
//
// #[cfg(test)]
// mod test {
// use types::Point;
// use algorithm::haversine_distance::HaversineDistance;
// #[test]
// fn distance3_test() {
// let dist = Point::new(-101.60, 37.43)
// .haversine_distance(&Point::new(-78.75, 40.97));
// assert_eq!(dist, 2004106.44144124);
// }
// }
44 changes: 0 additions & 44 deletions src/algorithm/haversine_distance.rs

This file was deleted.

0 comments on commit e26beeb

Please sign in to comment.