diff --git a/src/types/coords.rs b/src/types/coords.rs index 923405c..1bcb6ed 100644 --- a/src/types/coords.rs +++ b/src/types/coords.rs @@ -1,3 +1,4 @@ + use serde::{Deserialize, Serialize}; use std::f64::consts::PI; use std::hash::{Hash, Hasher}; @@ -20,14 +21,14 @@ impl CartesianCoords { let ra: f64; if self.y < 0.0 { - ra = (2.0 * PI) - self.x.acos(); + ra = (2.0 * PI) - self.x.clamp(-1.0, 1.0).acos(); } else { - ra = self.x.acos(); + ra = self.x.clamp(-1.0, 1.0).acos(); } EquatorialCoords { ra, - dec: self.z.asin(), + dec: self.z.clamp(-1.0, 1.0).asin(), } } } @@ -67,9 +68,10 @@ impl EquatorialCoords { } pub fn to_grid(&self, fov_size: f64) -> EquatorialCoords { + let clamped_size = fov_size.clamp(0.02, 1); EquatorialCoords { - ra: (self.ra / (2.0 * PI) * (1.0 - (2.0 * self.dec.abs() / PI)).powf(0.5) / fov_size).round(), - dec: (self.dec / 2.0 / fov_size).round(), + ra: (self.ra / (2.0 * PI) * (1.0 - (2.0 * self.dec.abs() / PI)).powf(0.5) / clamped_size).round(), + dec: (self.dec / (2.0 * PI) / clamped_size).round(), } } } @@ -88,5 +90,4 @@ impl Hash for EquatorialCoords { (self.ra as i32).hash(state); (self.dec as i32).hash(state); } -} - +} \ No newline at end of file