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
40
41
42
43
44
use num::Float;
use types::Point;
pub trait Distance<T, Rhs = Self>
{
fn distance(&self, rhs: &Rhs) -> T;
}
impl<T> Distance<T, Point<T>> for Point<T>
where T: Float
{
fn distance(&self, p: &Point<T>) -> T {
let (dx, dy) = (self.x() - p.x(), self.y() - p.y());
(dx * dx + dy * dy).sqrt()
}
}
#[cfg(test)]
mod test {
use types::Point;
use algorithm::distance::Distance;
#[test]
fn distance1_test() {
assert_eq!(Point::<f64>::new(0., 0.).distance(&Point::<f64>::new(1., 0.)), 1.);
}
#[test]
fn distance2_test() {
let dist = Point::new(-72.1235, 42.3521).distance(&Point::new(72.1260, 70.612));
assert!(dist < 147. && dist > 146.);
}
}