Last active
November 13, 2022 18:34
-
-
Save jnbek/4ecd266a4865335178ce8799fe142996 to your computer and use it in GitHub Desktop.
Get the distance between two degrees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let earth_radius_kilometer = 6371.0_f64; | |
let (paris_latitude_degrees, paris_longitude_degrees) = (48.85341_f64, -2.34880_f64); | |
let (london_latitude_degrees, london_longitude_degrees) = (51.50853_f64, -0.12574_f64); | |
let paris_latitude = paris_latitude_degrees.to_radians(); | |
let london_latitude = london_latitude_degrees.to_radians(); | |
let delta_latitude = (paris_latitude_degrees - london_latitude_degrees).to_radians(); | |
let delta_longitude = (paris_longitude_degrees - london_longitude_degrees).to_radians(); | |
let central_angle_inner = (delta_latitude / 2.0).sin().powi(2) | |
+ paris_latitude.cos() * london_latitude.cos() * (delta_longitude / 2.0).sin().powi(2); | |
let central_angle = 2.0 * central_angle_inner.sqrt().asin(); | |
let distance = earth_radius_kilometer * central_angle; | |
println!( | |
"Distance between Paris and London on the surface of Earth is {:.1} kilometers", | |
distance | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getEndpoint(lat1,lon1,bearing,d): | |
R = 6371 #Radius of the Earth | |
brng = math.radians(bearing) #convert degrees to radians | |
d = d*1.852 #convert nautical miles to km | |
lat1 = math.radians(lat1) #Current lat point converted to radians | |
lon1 = math.radians(lon1) #Current long point converted to radians | |
lat2 = math.asin( math.sin(lat1)*math.cos(d/R) + math.cos(lat1)*math.sin(d/R)*math.cos(brng)) | |
lon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),math.cos(d/R)-math.sin(lat1)*math.sin(lat2)) | |
lat2 = math.degrees(lat2) | |
lon2 = math.degrees(lon2) | |
return lat2,lon2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::io::{BufReader,BufRead}; | |
use std::fs::File; | |
fn main() { | |
let mut total_lines = 0; | |
let mut matched_lines = 0; | |
let args: Vec<_> = env::args().collect(); | |
if args.len() != 3 { | |
println!("{} filename string", args[0]); | |
return; | |
} | |
let input_path = ::std::env::args().nth(1).unwrap(); | |
let string_to_match = ::std::env::args().nth(2).unwrap(); | |
let file = BufReader::new(File::open(&input_path).unwrap()); | |
for line in file.lines() { | |
total_lines += 1; | |
let my_line = line.unwrap(); | |
if my_line.contains(&string_to_match) { | |
//println!("{}", my_line); | |
matched_lines += 1; | |
for vars in my_line.split(' ') { | |
println!("{}", vars); | |
let line = vars.split('=').nth(1).to_string(); | |
//let attrs: Vec<&str> = line.collect(); | |
//for attr in attrs { | |
println!("{}", line); | |
//} | |
} | |
} | |
} | |
println!("Lines processed: {}", total_lines); | |
println!("Lines matched: {}", matched_lines); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment