Add data generator (#20)

This commit is contained in:
Patrick Stevens
2023-06-03 20:04:13 +01:00
committed by GitHub
parent c2d09452b3
commit c80e6b6b1e
12 changed files with 472 additions and 112 deletions

11
haversine/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "haversine"
version = "0.1.0"
edition = "2021"
[lib]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.163", features = [ "derive" ] }

23
haversine/src/distance.rs Normal file
View File

@@ -0,0 +1,23 @@
use crate::haversine::CoordinatePair;
fn square(x: f64) -> f64 {
x * x
}
pub fn naive(point: &CoordinatePair, earth_radius: f64) -> f64 {
let lat1 = point.y0;
let lat2 = point.y1;
let lon1 = point.x0;
let lon2 = point.x1;
let lat_deg = (lat2 - lat1).to_radians();
let lon_deg = (lon2 - lon1).to_radians();
let lat1 = lat1.to_radians();
let lat2 = lat2.to_radians();
let a = square(f64::sin(lat_deg / 2.0))
+ f64::cos(lat1) * f64::cos(lat2) * square(f64::sin(lon_deg / 2.0));
let c = 2.0 * f64::asin(f64::sqrt(a));
earth_radius * c
}

1
haversine/src/earth.rs Normal file
View File

@@ -0,0 +1 @@
pub const RADIUS: f64 = 6372.8;

View File

@@ -0,0 +1,14 @@
use serde::Serialize;
#[derive(Serialize)]
pub struct CoordinatePair {
pub x0: f64,
pub y0: f64,
pub x1: f64,
pub y1: f64,
}
#[derive(Serialize)]
pub struct HaversineData {
pub pairs: Vec<CoordinatePair>,
}

3
haversine/src/lib.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod distance;
pub mod earth;
pub mod haversine;