Skip to content

Commit

Permalink
inline angle
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jun 5, 2024
1 parent 0f4b8fc commit cac060a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions geom/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,77 @@ impl Radians {
pub const TAU: Self = Radians(TAU);
pub const ZERO: Self = Radians(0.0);

#[inline]
pub fn vec2(self) -> Vec2 {
Vec2 {
x: self.0.cos(),
y: self.0.sin(),
}
}

#[inline]
pub fn from_deg(deg: f32) -> Self {
Self(deg * (PI / 180.0))
}

#[inline]
pub fn normalize(&mut self) {
self.0 %= TAU;
}

#[inline]
pub fn cos(self) -> f32 {
self.0.cos()
}

#[inline]
pub fn sin(self) -> f32 {
self.0.sin()
}

#[inline]
pub fn min(self, other: Self) -> Self {
Self(self.0.min(other.0))
}

#[inline]
pub fn max(self, other: Self) -> Self {
Self(self.0.max(other.0))
}

#[inline]
pub fn to_degrees(self) -> Degrees {
Degrees(self.0 * (180.0 / PI))
}
}

impl Degrees {
#[inline]
pub fn vec2(self) -> Vec2 {
Radians::from(self).vec2()
}

#[inline]
pub fn normalize(&mut self) {
self.0 %= 360.0;
}

#[inline]
pub fn min(self, other: Self) -> Self {
Self(self.0.min(other.0))
}

#[inline]
pub fn max(self, other: Self) -> Self {
Self(self.0.max(other.0))
}

#[inline]
pub fn to_radians(self) -> Radians {
Radians(self.0 * (PI / 180.0))
}

#[inline]
pub fn from_rad(rad: f32) -> Self {
Self(rad * (180.0 / PI))
}
Expand Down Expand Up @@ -146,12 +160,14 @@ impl AddAssign for Radians {
}

impl From<Radians> for Degrees {
#[inline]
fn from(r: Radians) -> Self {
Self(r.0 * (180.0 / PI))
}
}

impl From<Degrees> for Radians {
#[inline]
fn from(r: Degrees) -> Self {
Self(r.0 * (PI / 180.0))
}
Expand All @@ -160,6 +176,7 @@ impl From<Degrees> for Radians {
impl Mul<f32> for Radians {
type Output = Self;

#[inline]
fn mul(self, rhs: f32) -> Self::Output {
Self(self.0 * rhs)
}
Expand All @@ -168,30 +185,35 @@ impl Mul<f32> for Radians {
impl Neg for Radians {
type Output = Self;

#[inline]
fn neg(self) -> Self::Output {
Self(-self.0)
}
}

impl From<f32> for Radians {
#[inline]
fn from(v: f32) -> Self {
Self(v)
}
}

impl From<f32> for Degrees {
#[inline]
fn from(v: f32) -> Self {
Self(v)
}
}

impl From<Degrees> for f32 {
#[inline]
fn from(d: Degrees) -> Self {
d.0
}
}

impl From<Radians> for f32 {
#[inline]
fn from(r: Radians) -> Self {
r.0
}
Expand Down

0 comments on commit cac060a

Please sign in to comment.