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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::Point;
use num_traits::Float;
pub trait Bearing<T: Float> {
fn bearing(&self, point: Point<T>) -> T;
}
impl<T> Bearing<T> for Point<T>
where
T: Float,
{
fn bearing(&self, point: Point<T>) -> T {
let (lng_a, lat_a) = (self.x().to_radians(), self.y().to_radians());
let (lng_b, lat_b) = (point.x().to_radians(), point.y().to_radians());
let delta_lng = lng_b - lng_a;
let s = lat_b.cos() * delta_lng.sin();
let c = lat_a.cos() * lat_b.sin() - lat_a.sin() * lat_b.cos() * delta_lng.cos();
T::atan2(s, c).to_degrees()
}
}
#[cfg(test)]
mod test {
use crate::algorithm::bearing::Bearing;
use crate::algorithm::haversine_destination::HaversineDestination;
use crate::point;
#[test]
fn returns_the_proper_bearing_to_another_point() {
let p_1 = point!(x: 9.177789688110352f64, y: 48.776781529534965);
let p_2 = p_1.haversine_destination(45., 10000.);
let b_1 = p_1.bearing(p_2);
assert_relative_eq!(b_1, 45., epsilon = 1.0e-6);
let p_3 = point!(x: 9., y: 47.);
let p_4 = point!(x: 9., y: 48.);
let b_2 = p_3.bearing(p_4);
assert_relative_eq!(b_2, 0., epsilon = 1.0e-6);
}
}