From ae8efcfae121d32447ad5c751d83b3527d13f75e Mon Sep 17 00:00:00 2001 From: T0mstone Date: Thu, 26 May 2022 14:11:55 +0200 Subject: [PATCH 1/3] Improve the `Angle` type * Add two new units * Replace usage of `2 * PI` by `TAU` * Replace angle-angle mul./div. with angle-scalar mul./div. --- crates/fj/src/angle.rs | 56 +++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/crates/fj/src/angle.rs b/crates/fj/src/angle.rs index 5a6389d8d..e369fc6c8 100644 --- a/crates/fj/src/angle.rs +++ b/crates/fj/src/angle.rs @@ -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)] @@ -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 @@ -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 } @@ -72,30 +91,37 @@ impl std::ops::SubAssign for Angle { } } -impl std::ops::Mul for Angle { +impl std::ops::Mul for Angle { + type Output = Angle; + fn mul(self, rhs: f64) -> Self::Output { + Self::from_rad(self.rad * rhs) + } +} + +impl std::ops::Mul 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 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 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 for Angle { + fn div_assign(&mut self, rhs: f64) { + self.rad /= rhs; self.wrap_assign() } } From 0b46488a3bc3431a9f20a0906d3a389be2bbed07 Mon Sep 17 00:00:00 2001 From: T0mstone Date: Thu, 26 May 2022 14:16:51 +0200 Subject: [PATCH 2/3] Reintroduce angle division --- crates/fj/src/angle.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/fj/src/angle.rs b/crates/fj/src/angle.rs index e369fc6c8..880941c97 100644 --- a/crates/fj/src/angle.rs +++ b/crates/fj/src/angle.rs @@ -125,3 +125,10 @@ impl std::ops::DivAssign for Angle { self.wrap_assign() } } + +impl std::ops::Div for Angle { + type Output = f64; + fn div(self, rhs: Angle) -> Self::Output { + self.rad / rhs.rad + } +} \ No newline at end of file From d2a25ccfd8bc2be2be15543aa1d54b6bc7a1415a Mon Sep 17 00:00:00 2001 From: T0mstone Date: Thu, 26 May 2022 14:27:14 +0200 Subject: [PATCH 3/3] Add newline at the end of file --- crates/fj/src/angle.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj/src/angle.rs b/crates/fj/src/angle.rs index 880941c97..5345f9cec 100644 --- a/crates/fj/src/angle.rs +++ b/crates/fj/src/angle.rs @@ -131,4 +131,4 @@ impl std::ops::Div for Angle { fn div(self, rhs: Angle) -> Self::Output { self.rad / rhs.rad } -} \ No newline at end of file +}