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

Remove default Angle wrapping behavior #1478

Merged
merged 1 commit into from
Jan 4, 2023
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
22 changes: 9 additions & 13 deletions crates/fj/src/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ const GON_RAD: f64 = PI / 200.;
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Angle {
// The value of the angle in radians
/// The value of the angle in radians
rad: f64,
}

impl Angle {
/// Create a new angle specified in radians
pub fn from_rad(rad: f64) -> Self {
Self {
rad: Self::wrap(rad),
}
Self { rad }
}
/// Create a new angle specified in degrees
pub fn from_deg(deg: f64) -> Self {
Expand Down Expand Up @@ -47,6 +45,13 @@ impl Angle {
self.rad / GON_RAD
}

/// Returns this angle normalized to the range [0, 2pi) radians
pub fn normalized(&self) -> Self {
Self {
rad: Self::wrap(self.rad),
}
}

// ensures that the angle is always 0 <= a < 2*pi
fn wrap(rad: f64) -> f64 {
let modulo = rad % TAU;
Expand All @@ -56,11 +61,6 @@ impl Angle {
modulo
}
}

// ensures that the angle is always 0 <= a < 2*pi
fn wrap_assign(&mut self) {
self.rad = Self::wrap(self.rad);
}
}

impl std::ops::Add for Angle {
Expand All @@ -73,7 +73,6 @@ impl std::ops::Add for Angle {
impl std::ops::AddAssign for Angle {
fn add_assign(&mut self, rhs: Self) {
self.rad += rhs.rad;
self.wrap_assign();
}
}

Expand All @@ -87,7 +86,6 @@ impl std::ops::Sub for Angle {
impl std::ops::SubAssign for Angle {
fn sub_assign(&mut self, rhs: Self) {
self.rad -= rhs.rad;
self.wrap_assign();
}
}

Expand All @@ -108,7 +106,6 @@ impl std::ops::Mul<Angle> for f64 {
impl std::ops::MulAssign<f64> for Angle {
fn mul_assign(&mut self, rhs: f64) {
self.rad *= rhs;
self.wrap_assign();
}
}

Expand All @@ -122,7 +119,6 @@ impl std::ops::Div<f64> for Angle {
impl std::ops::DivAssign<f64> for Angle {
fn div_assign(&mut self, rhs: f64) {
self.rad /= rhs;
self.wrap_assign();
}
}

Expand Down