nya-core 0.2.0

nya core library
Documentation
//! Basic arithmetic utilities

use num_traits::Float;

pub mod sizes;

/// Linear interpolation, see
/// [Linear Interpolation on Wikipedia](https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support)
/// for more information.
///
/// The expression being used is: `(1 - t) * a + t * b`
///
/// Example:
///
/// ```
/// use nya_core::math::lerp;
/// println!("{}", lerp(0.0, 100.0, 0.25)); // 25
/// ```
#[doc(alias = "mix")]
pub fn lerp<T: Float>(a: T, b: T, t: T) -> T {
    (T::one() - t) * a + t * b
}

/// Generic floating-point angle, can be represented in Degrees or Radians.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Angle<T: Float> {
    /// Degrees
    Deg(T),

    /// Radians
    Rad(T),
}

impl<T: Float> Angle<T> {
    /// Convert the wrapped value to degrees
    pub fn degrees(self) -> T {
        match self {
            Self::Deg(d) => d,
            Self::Rad(r) => r.to_degrees(),
        }
    }

    /// Convert the wrapped value to radians
    pub fn radians(self) -> T {
        match self {
            Self::Deg(d) => d.to_radians(),
            Self::Rad(r) => r,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lerp() {
        assert_eq!(lerp(0., 100., 0.25), 25.0);
        assert_eq!(lerp(0., 100., 1.435), 143.5);
    }
}