Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Angle #641

Merged
merged 3 commits into from
May 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions crates/fj/src/angle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::f64::consts::PI;
use std::f64::consts::{PI, TAU};

// One gon in radians
const GON_RAD: f64 = PI / 200.;

/// An angle
#[derive(Copy, Clone, Debug)]
Expand All @@ -19,6 +22,14 @@ impl Angle {
pub fn from_deg(deg: f64) -> Self {
Self::from_rad(deg.to_radians())
}
/// Create a new angle specified in [revolutions](https://en.wikipedia.org/wiki/Turn_(angle))
pub fn from_rev(rev: f64) -> Self {
Self::from_rad(rev * TAU)
}
/// Create a new angle specified in [gon](https://en.wikipedia.org/wiki/Gradian)
pub fn from_gon(gon: f64) -> Self {
Self::from_rad(gon * GON_RAD)
}
/// Retrieve value of angle as radians
pub fn rad(&self) -> f64 {
self.rad
Expand All @@ -27,12 +38,20 @@ impl Angle {
pub fn deg(&self) -> f64 {
self.rad.to_degrees()
}
/// Retrieve value of angle as [revolutions](https://en.wikipedia.org/wiki/Turn_(angle))
pub fn rev(&self) -> f64 {
self.rad / TAU
}
/// Retrieve value of angle as [gon](https://en.wikipedia.org/wiki/Gradian)
pub fn gon(&self) -> f64 {
self.rad / GON_RAD
}

// ensures that the angle is always 0 <= a < 2*pi
fn wrap(rad: f64) -> f64 {
let modulo = rad % (2. * PI);
let modulo = rad % TAU;
if modulo < 0. {
2. * PI + modulo
TAU + modulo
} else {
modulo
}
Expand Down Expand Up @@ -72,30 +91,44 @@ impl std::ops::SubAssign for Angle {
}
}

impl std::ops::Mul for Angle {
impl std::ops::Mul<f64> for Angle {
type Output = Angle;
fn mul(self, rhs: f64) -> Self::Output {
Self::from_rad(self.rad * rhs)
}
}

impl std::ops::Mul<Angle> for f64 {
type Output = Angle;
fn mul(self, rhs: Self) -> Self::Output {
Self::from_rad(self.rad * rhs.rad)
fn mul(self, rhs: Angle) -> Self::Output {
rhs * self
}
}

impl std::ops::MulAssign for Angle {
fn mul_assign(&mut self, rhs: Self) {
self.rad *= rhs.rad;
impl std::ops::MulAssign<f64> for Angle {
fn mul_assign(&mut self, rhs: f64) {
self.rad *= rhs;
self.wrap_assign()
}
}

impl std::ops::Div for Angle {
impl std::ops::Div<f64> for Angle {
type Output = Angle;
fn div(self, rhs: Self) -> Self::Output {
Self::from_rad(self.rad / rhs.rad)
fn div(self, rhs: f64) -> Self::Output {
Self::from_rad(self.rad / rhs)
}
}

impl std::ops::DivAssign for Angle {
fn div_assign(&mut self, rhs: Self) {
self.rad /= rhs.rad;
impl std::ops::DivAssign<f64> for Angle {
fn div_assign(&mut self, rhs: f64) {
self.rad /= rhs;
self.wrap_assign()
}
}

impl std::ops::Div for Angle {
type Output = f64;
fn div(self, rhs: Angle) -> Self::Output {
self.rad / rhs.rad
}
}