haversine implementation

main
Zynh0722 2024-04-23 00:11:21 -07:00
parent e4d787b497
commit 2d835af3ac
1 changed files with 17 additions and 0 deletions

17
src/distance.rs Normal file
View File

@ -0,0 +1,17 @@
const EARTH_RAD_METERS: f64 = 6371e3;
type LatLon = (f64, f64);
pub fn haversine(a: LatLon, b: LatLon) -> f64 {
let a_latitude = a.0.to_radians();
let b_latitude = b.0.to_radians();
let delta_latitude = (b.0 - a.0).to_radians();
let delta_longitude = (b.1 - a.1).to_radians();
let central_angle_inner = (delta_latitude / 2.0).sin().powi(2)
+ a_latitude.cos() * b_latitude.cos() * (delta_longitude / 2.0).sin().powi(2);
let central_angle = 2.0 * central_angle_inner.sqrt().asin();
EARTH_RAD_METERS * central_angle
}