feat: use the haversine formula to get distance from lightning to you
This commit is contained in:
parent
47dbbb98f5
commit
10cde9881d
1 changed files with 23 additions and 2 deletions
25
src/main.rs
25
src/main.rs
|
|
@ -48,9 +48,25 @@ fn in_bounds(lat: f64, lon: f64, center_lat: f64, center_lon: f64, delta: f64) -
|
|||
(lat >= lat_min && lat <= lat_max) && (lon >= lon_min && lon <= lon_max)
|
||||
}
|
||||
|
||||
fn haversine_distance(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
|
||||
let r = 6371.0; // Earth radius in km
|
||||
let dlat = (lat2 - lat1).to_radians();
|
||||
let dlon = (lon2 - lon1).to_radians();
|
||||
let a = (dlat / 2.0).sin().powi(2)
|
||||
+ lat1.to_radians().cos() * lat2.to_radians().cos() * (dlon / 2.0).sin().powi(2);
|
||||
let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
|
||||
r * c
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args = Args::parse();
|
||||
println!("Boot arguments:");
|
||||
println!(" center_lat: {}", args.center_lat);
|
||||
println!(" center_lon: {}", args.center_lon);
|
||||
println!(" delta: {}", args.delta);
|
||||
println!(" ntfy_topic: {}", args.ntfy_topic);
|
||||
println!(" detector_fetch: {}", args.detector_fetch);
|
||||
|
||||
CryptoProvider::install_default(rustls_rustcrypto::provider())
|
||||
.expect("install rustcrypto provider");
|
||||
|
|
@ -155,9 +171,12 @@ async fn main() {
|
|||
let lon = all.lon + all.lonc as f64;
|
||||
let lat = all.lat + all.latc as f64;
|
||||
if in_bounds(lat, lon, args.center_lat, args.center_lon, args.delta) {
|
||||
let distance_km =
|
||||
haversine_distance(args.center_lat, args.center_lon, lat, lon);
|
||||
|
||||
println!(
|
||||
"Delay: {}ms, offset to now: {}ms, status: {}, region: {} | \n Strike at: {}, {}",
|
||||
all.delay, offset_ms, all.status, all.region, lon, lat
|
||||
"Delay: {}ms, offset to now: {}ms, status: {}, region: {} | \n Strike at: {}, {} | Distance to you: {:.2} km",
|
||||
all.delay, offset_ms, all.status, all.region, lon, lat, distance_km
|
||||
);
|
||||
|
||||
let mut detector_details: Vec<String> = Vec::new();
|
||||
|
|
@ -199,6 +218,7 @@ async fn main() {
|
|||
• Status: {}\n\
|
||||
• Region: {}\n\
|
||||
• Location: {:.5}, {:.5}\n\
|
||||
• Distance to you: {:.2} km\n\
|
||||
• Used detectors:\n{}\n\
|
||||
Stay safe! 🌩️",
|
||||
all.delay,
|
||||
|
|
@ -206,6 +226,7 @@ async fn main() {
|
|||
all.region,
|
||||
lon,
|
||||
lat,
|
||||
distance_km,
|
||||
{
|
||||
if args.detector_fetch {
|
||||
let shown = 2;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue