Skip to content

Commit

Permalink
Add more methods (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamGallagher737 authored Nov 20, 2024
1 parent 85e99e7 commit e10dcc4
Showing 1 changed file with 101 additions and 2 deletions.
103 changes: 101 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ impl Angle {
Angle { value: normalized }
}

/// Create a new [`Angle`] from degrees.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let angle = Angle::from_degrees(180.0);
/// assert_eq!(angle.radians(), PI);
/// ```
pub fn from_degrees(degrees: f64) -> Angle {
Angle::new(degrees.to_radians())
}

/// Get the angle in radians.
///
/// ```
Expand Down Expand Up @@ -91,20 +103,60 @@ impl Angle {
}
}

/// Get the difference between this angle and another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(-PI / 2.0);
/// let b = Angle::new(PI / 2.0);
/// assert_eq!(a.difference(&b).radians(), PI);
/// ```
#[inline(always)]
pub fn difference(&self, other: &Self) -> Self {
Angle::new(other.value - self.value)
}

/// Get the absolute distance between this angle and another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(-PI);
/// let b = Angle::new(PI / 2.0);
/// assert_eq!(a.abs_distance(&b).radians(), PI / 2.0);
/// assert_eq!(a.distance(&b).radians(), PI / 2.0);
/// ```
#[inline(always)]
pub fn abs_distance(&self, other: &Self) -> Self {
pub fn distance(&self, other: &Self) -> Self {
Angle::new(self.value - other.value).abs()
}

/// Check if this angle is in a clockwise driection from another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(PI);
/// let b = Angle::new(PI / 2.0);
/// assert!(a.is_clockwise_to(&b));
/// ```
pub fn is_clockwise_to(&self, other: &Self) -> bool {
self.difference(other).value < 0.0
}

/// Check if this angle is in a counterclockwise driection from another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(PI / 2.0);
/// let b = Angle::new(PI);
/// assert!(a.is_counterclockwise_to(&b));
/// ```
pub fn is_counterclockwise_to(&self, other: &Self) -> bool {
self.difference(other).value > 0.0
}

/// Clamp the angle to a given range.
///
/// ```
Expand All @@ -124,6 +176,22 @@ impl Angle {
}
}

/// Interpolate between this angle and another by a given factor (t).
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(0.0);
/// let b = Angle::new(PI);
/// let interpolated = a.lerp(&b, 0.5);
/// assert_eq!(interpolated.radians(), PI / 2.0);
/// ```
pub fn lerp(&self, other: &Self, t: f64) -> Angle {
assert!((0.0..=1.0).contains(&t), "t must be between 0.0 and 1.0");
let difference = self.difference(other);
Angle::new(self.value + difference.value * t)
}

/// Get the opposite angle (add π).
///
/// ```
Expand All @@ -136,6 +204,20 @@ impl Angle {
Angle::new(self.value + PI)
}

/// Find the midpoint angle between this angle and another one.
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::PI;
/// let a = Angle::new(0.0);
/// let b = Angle::new(PI);
/// assert_eq!(a.midpoint(&b).radians(), PI / 2.0);
/// ```
pub fn midpoint(&self, other: &Self) -> Self {
let diff = self.difference(other).value;
Angle::new(self.value + diff / 2.0)
}

/// Get the unit vector of this angle defined as (cos(θ), sin(θ)).
///
/// ```
Expand All @@ -154,6 +236,23 @@ impl Angle {
#[cfg(feature = "libm")]
return (libm::cos(self.value), libm::sin(self.value));
}

/// Create an angle from a unit vector (x, y).
///
/// ```
/// # use radian::Angle;
/// # use core::f64::consts::{PI, SQRT_2};
/// # use approx::assert_relative_eq;
/// let angle = Angle::from_unit_vector(SQRT_2 / 2.0, SQRT_2 / 2.0);
/// assert_relative_eq!(angle.radians(), PI / 4.0);
/// ```
#[cfg(any(feature = "std", feature = "libm"))]
pub fn from_unit_vector(x: f64, y: f64) -> Angle {
#[cfg(feature = "std")]
return Angle::new(y.atan2(x));
#[cfg(feature = "libm")]
return Angle::new(libm::atan2(y, x));
}
}

impl Display for Angle {
Expand Down

0 comments on commit e10dcc4

Please sign in to comment.