41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
use arrayvec::ArrayString;
|
|
|
|
const FIELD_SYMBOLS: &str = "ABCDEFGHIJKLMNOPQR";
|
|
const SUBSQUARE_SYMBOLS: &str = "abcdefghijklmnopqrstuvwx";
|
|
|
|
pub fn locator_from_coordinates(lat: f64, long: f64) -> ArrayString<6> {
|
|
let mut target_buf = ArrayString::<6>::new();
|
|
|
|
let false_east = if long < 180.0 {
|
|
long + 180.0
|
|
} else {
|
|
long - 180.0
|
|
} as f64;
|
|
|
|
let false_north = if lat < 90.0 { lat + 90.0 } else { lat - 90.0 } as f32;
|
|
|
|
let long_field = (false_east / 20.0) as usize;
|
|
let mut long_rest = false_east % 20.0;
|
|
target_buf.push(FIELD_SYMBOLS.chars().nth(long_field).unwrap());
|
|
|
|
let lat_field = (false_north / 10.0) as usize;
|
|
let mut lat_rest = false_north % 10.0;
|
|
target_buf.push(FIELD_SYMBOLS.chars().nth(lat_field).unwrap());
|
|
|
|
let long_square = (long_rest / 2.0) as u32;
|
|
long_rest = long_rest % 2.0;
|
|
target_buf.push(char::from_digit(long_square, 10).unwrap());
|
|
|
|
let lat_square = (lat_rest / 1.0) as u32;
|
|
lat_rest = lat_rest % 1.0;
|
|
target_buf.push(char::from_digit(lat_square, 10).unwrap());
|
|
|
|
let long_subsquare = (long_rest / 2.0 * 24.0) as usize;
|
|
target_buf.push(SUBSQUARE_SYMBOLS.chars().nth(long_subsquare).unwrap());
|
|
|
|
let lat_subsquare = (lat_rest / 1.0 * 24.0) as usize;
|
|
target_buf.push(SUBSQUARE_SYMBOLS.chars().nth(lat_subsquare).unwrap());
|
|
|
|
return target_buf;
|
|
}
|