pub struct Angle { /* private fields */ }Expand description
An angular measurement stored as radians.
Angle is the primary type for representing angles throughout this library.
It stores the angle as a 64-bit float in radians and provides conversions to/from
other angular units commonly used in astronomy.
§Internal Representation
Angles are stored as radians (f64). This choice optimizes for:
- Direct use with trigonometric functions
- Precision in intermediate calculations
- Consistency with mathematical conventions
§Derives
Copy,Clone: Angles are small (8 bytes) and cheap to copyDebug: Shows internal radian valuePartialEq,PartialOrd: Compare angles directly (compares radian values)
Note: Eq and Ord are not implemented because f64 can be NaN.
Implementations§
Source§impl Angle
impl Angle
Sourcepub const HALF_PI: Self
pub const HALF_PI: Self
Pi/2 radians (90 degrees). Useful for right angles and pole declinations.
Sourcepub const fn from_radians(rad: f64) -> Self
pub const fn from_radians(rad: f64) -> Self
Creates an angle from radians.
This is the only const constructor because radians are the internal representation.
§Example
use celestial_core::Angle;
use std::f64::consts::FRAC_PI_4;
let angle = Angle::from_radians(FRAC_PI_4);
assert!((angle.degrees() - 45.0).abs() < 1e-10);Sourcepub fn from_degrees(deg: f64) -> Self
pub fn from_degrees(deg: f64) -> Self
Creates an angle from degrees.
§Example
use celestial_core::Angle;
let angle = Angle::from_degrees(180.0);
assert!((angle.radians() - celestial_core::constants::PI).abs() < 1e-10);Sourcepub fn from_hours(h: f64) -> Self
pub fn from_hours(h: f64) -> Self
Creates an angle from hours.
In astronomy, right ascension is measured in hours where 24h = 360 degrees. Each hour equals 15 degrees.
§Example
use celestial_core::Angle;
let ra = Angle::from_hours(6.0); // 6h = 90 degrees
assert!((ra.degrees() - 90.0).abs() < 1e-10);
let ra_24h = Angle::from_hours(24.0); // Full circle
assert!((ra_24h.degrees() - 360.0).abs() < 1e-10);Sourcepub fn from_arcseconds(arcsec: f64) -> Self
pub fn from_arcseconds(arcsec: f64) -> Self
Creates an angle from arcseconds.
One arcsecond = 1/3600 of a degree. Commonly used for:
- Parallax measurements
- Proper motion
- Small angular separations
§Example
use celestial_core::Angle;
let angle = Angle::from_arcseconds(3600.0); // 1 degree
assert!((angle.degrees() - 1.0).abs() < 1e-10);
// Proxima Centauri's parallax is about 0.77 arcseconds
let parallax = Angle::from_arcseconds(0.77);Sourcepub fn from_arcminutes(arcmin: f64) -> Self
pub fn from_arcminutes(arcmin: f64) -> Self
Creates an angle from arcminutes.
One arcminute = 1/60 of a degree. Commonly used for:
- Field of view specifications
- Object sizes (e.g., the Moon is about 31 arcminutes)
§Example
use celestial_core::Angle;
let angle = Angle::from_arcminutes(60.0); // 1 degree
assert!((angle.degrees() - 1.0).abs() < 1e-10);
// Full Moon's apparent diameter
let moon_diameter = Angle::from_arcminutes(31.0);Sourcepub fn radians(self) -> f64
pub fn radians(self) -> f64
Returns the angle in radians.
This is the internal representation, so no conversion occurs.
Sourcepub fn hours(self) -> f64
pub fn hours(self) -> f64
Returns the angle in hours.
Useful for right ascension where 24h = 360 degrees.
Sourcepub fn arcseconds(self) -> f64
pub fn arcseconds(self) -> f64
Returns the angle in arcseconds.
Sourcepub fn arcminutes(self) -> f64
pub fn arcminutes(self) -> f64
Returns the angle in arcminutes.
Sourcepub fn sin_cos(self) -> (f64, f64)
pub fn sin_cos(self) -> (f64, f64)
Returns both sine and cosine of the angle.
Convenience method when you need both values.
§Returns
A tuple (sin, cos).
§Example
use celestial_core::Angle;
let angle = Angle::from_degrees(30.0);
let (sin, cos) = angle.sin_cos();
assert!((sin - 0.5).abs() < 1e-10);
assert!((cos - 0.866025).abs() < 1e-5);Sourcepub fn abs(self) -> Self
pub fn abs(self) -> Self
Returns the absolute value of the angle.
§Example
use celestial_core::Angle;
let negative = Angle::from_degrees(-45.0);
let absolute = negative.abs();
assert!((absolute.degrees() - 45.0).abs() < 1e-10);Sourcepub fn wrapped(self) -> Self
pub fn wrapped(self) -> Self
Wraps the angle to the range [-pi, +pi) (i.e., [-180, +180) degrees).
Use this for longitude-like quantities or angular differences where you want the shortest arc representation.
§Example
use celestial_core::Angle;
let angle = Angle::from_degrees(270.0);
let wrapped = angle.wrapped();
assert!((wrapped.degrees() - (-90.0)).abs() < 1e-10);
let angle2 = Angle::from_degrees(-270.0);
let wrapped2 = angle2.wrapped();
assert!((wrapped2.degrees() - 90.0).abs() < 1e-10);Sourcepub fn normalized(self) -> Self
pub fn normalized(self) -> Self
Normalizes the angle to the range [0, 2*pi) (i.e., [0, 360) degrees).
Use this for right ascension or any angle that should be non-negative.
§Example
use celestial_core::Angle;
let angle = Angle::from_degrees(-90.0);
let normalized = angle.normalized();
assert!((normalized.degrees() - 270.0).abs() < 1e-10);
let angle2 = Angle::from_degrees(450.0);
let normalized2 = angle2.normalized();
assert!((normalized2.degrees() - 90.0).abs() < 1e-10);Sourcepub fn validate_longitude(self, normalize: bool) -> Result<Self, AstroError>
pub fn validate_longitude(self, normalize: bool) -> Result<Self, AstroError>
Validates the angle as a longitude.
If normalize is true, wraps to [0, 2*pi) and returns Ok.
If normalize is false, requires the angle to be in [-pi, +pi] or returns Err.
§Errors
Returns AstroError if:
- The angle is not finite (NaN or infinity)
normalizeis false and the angle is outside [-180, +180] degrees
Sourcepub fn validate_latitude(self) -> Result<Self, AstroError>
pub fn validate_latitude(self) -> Result<Self, AstroError>
Validates the angle as a geographic latitude.
Latitude must be in [-90, +90] degrees ([-pi/2, +pi/2] radians).
§Errors
Returns AstroError if:
- The angle is not finite (NaN or infinity)
- The angle is outside [-90, +90] degrees
Sourcepub fn validate_declination(self, beyond_pole: bool) -> Result<Self, AstroError>
pub fn validate_declination(self, beyond_pole: bool) -> Result<Self, AstroError>
Validates the angle as a declination.
beyond_pole = false: standard range [-90°, +90°]beyond_pole = true: extended range [-180°, +180°] for GEM pier-flipped observations
§Errors
Returns AstroError if:
- The angle is not finite (NaN or infinity)
- The angle is outside the valid range
Sourcepub fn validate_right_ascension(self) -> Result<Self, AstroError>
pub fn validate_right_ascension(self) -> Result<Self, AstroError>
Validates the angle as a right ascension, normalizing to [0, 360) degrees.
Unlike declination, right ascension is cyclic. This method accepts any finite angle and normalizes it to [0, 2*pi).
§Errors
Returns AstroError if the angle is not finite (NaN or infinity).