rkepler 0.4.2

Astronomical almanac algorithms for the Rust
Documentation
/*
Copyright (c) 2022 Peter Hristozov / Петър Христозов

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//! Operation on angles.

use std;

/// Value of Pi.
pub const PI: f64 = std::f64::consts::PI;
/// Value of 2.0*Pi.
pub const DBLPI: f64 = std::f64::consts::TAU;
/// Value of Pi/2.0.
pub const HLFPI: f64 = std::f64::consts::FRAC_PI_2;
/// Arc seconds in a full circle.
pub const TURNAS: f64 = 1296000.0;
/// Seconds in a full circle.
pub const TURNS: f64 = 86400.0;
/// Degrees to radians.
pub const DEGRAD: f64 = std::f64::consts::PI / 180.0;
/// Radians to degrees.
pub const RADDEG: f64 = 180.0 / std::f64::consts::PI;
/// Arc seconds to radians.
pub const ASECRAD: f64 = DBLPI / TURNAS;
/// Radians to arc seconds.
pub const RADASEC: f64 = TURNAS / DBLPI;
/// Seconds to radians.
pub const SECRAD: f64 = DBLPI / TURNS;
/// Radians to seconds.
pub const RADSEC: f64 = TURNS / DBLPI;

/// Normalize angle into the range 0 <= a < 2pi.
/// # Arguments
/// * `ang` angle in radians
/// # Returns
/// angle in range 0-2pi
pub fn norm_dblpi(ang: f64) -> f64 {
    let out = ang % DBLPI as f64;
    if out < 0.0 { out + DBLPI } else { out }
}

/// Normalize angle into the range -pi <= a < +pi.
/// # Arguments
/// * `ang` angle in radians
/// # Returns
/// angle in range +/-pi
pub fn norm_pi(ang: f64) -> f64 {
    let out = ang % DBLPI as f64;
    if out.abs() >= PI {
        out - out.signum() * DBLPI
    } else {
        out
    }
}

/// Convert degrees, arcminutes, arcseconds to radians.
/// # Arguments
/// * `sign` '-' = negative, otherwise positive
/// * `deg` degrees
/// * `amin` arcminutes
/// * `asec` arcseconds
/// # Returns
/// angle in radians
pub fn dms_rad(sign: char, deg: f64, amin: f64, asec: f64) -> f64 {
    let out = (60.0 * ((60.0 * deg) + amin) + asec) * ASECRAD;
    if sign == '-' { -out } else { out }
}

/// Convert hours, minutes, seconds to radians.
/// # Arguments
/// * `hour` hours
/// * `min` minutes
/// * `sec` seconds
/// # Returns
/// angle in radians
pub fn hms_rad(hour: f64, min: f64, sec: f64) -> f64 {
    (60.0 * ((60.0 * hour) + min) + sec) * SECRAD
}

/// Decompose radians into degrees, arcminutes, arcseconds, fraction.
/// # Arguments
/// * `angle` angle in radians
/// * `ndp` resolution -4..9
/// # Returns
/// sign, degrees, arcminutes, arcseconds, fraction
pub fn rad_dms(angle: f64, ndp: i8) -> (char, i32, i32, i32, i32) {
    let sign = if angle >= 0.0 { '+' } else { '-' };
    let mut a = angle.abs() * RADASEC;

    let mut nrs: i32 = 1;
    if ndp < 0 {
        for n in 0..-ndp {
        nrs *= if (n == 0) || (n == 2) { 6 } else { 10 };
        }
        let rs = nrs as f64;
        let w = a / rs;
        a = rs * w.round();
    }
    if ndp > 0 {
        nrs = 10_i32.pow(ndp as u32);
    }
    let rs = nrs as f64;
    let rm = rs * 60.0;
    let rd = rm * 60.0;

    a = (rs * a).round();

    let ad = a / rd;
    a -= ad.trunc() * rd;
    let am = a / rm;
    a -= am.trunc() * rm;
    let asc = a / rs;
    let asf = a - asc.trunc() * rs;

    (sign, ad as i32, am as i32, asc as i32, asf as i32)
}

/// Decompose radians into hours, minutes, seconds, fraction.
/// # Arguments
/// * `angle` angle in radians
/// * `ndp` resolution -4..9
/// # Returns
/// sign, hours, minutes, seconds, fraction
pub fn rad_hms(angle: f64, ndp: i8) -> (char, i32, i32, i32, i32) {
    rad_dms(angle / 15.0, ndp)
}

/// Decompose day's fraction to hours, minutes, seconds, fraction.
/// # Arguments
/// * `dayfr` fraction of day
/// * `ndp` resolution -4..9
/// # Returns
/// sign, hours, minutes, seconds, fraction
pub fn day_hms(dayfr: f64, ndp: i8) -> (char, i32, i32, i32, i32) {
    rad_dms(dayfr * DBLPI / 15.0, ndp)
}