Angle: add the normalize_signed method

This method is like `normalize` except that it normalizes to have an
absolute value of no more than `turn_div_2`.
This commit is contained in:
Ben Boeckel 2017-11-02 11:36:55 -04:00
parent a570349a0d
commit 2d2c501074
2 changed files with 26 additions and 1 deletions

View file

@ -603,6 +603,13 @@ where
}
}
/// Return the angle, normalized to the range `[-turn_div_2, turn_div_2)`.
#[inline]
fn normalize_signed(self) -> Self {
let rem = self.normalize();
if Self::turn_div_2() < rem { rem - Self::full_turn() } else { rem }
}
/// Return the angle rotated by half a turn.
#[inline]
fn opposite(self) -> Self {

View file

@ -17,7 +17,25 @@
extern crate approx;
extern crate cgmath;
use cgmath::{Deg, Rad};
use cgmath::{Angle, Deg, Rad};
#[test]
fn test_normalize_signed() {
let angle: Rad<f64> = Rad::full_turn().normalize_signed();
assert_ulps_eq!(&angle, &Rad(0f64));
let angle: Rad<f64> = (Rad::full_turn() + Rad::turn_div_4()).normalize_signed();
assert_ulps_eq!(&angle, &Rad::turn_div_4());
let angle: Rad<f64> = (Rad::full_turn() - Rad::turn_div_4()).normalize_signed();
assert_ulps_eq!(&angle, &-Rad::turn_div_4());
let angle: Rad<f64> = Rad::turn_div_2().normalize_signed();
assert_ulps_eq!(&angle, &Rad::turn_div_2());
let angle: Rad<f64> = (-Rad::turn_div_2()).normalize_signed();
assert_ulps_eq!(&angle, &Rad::turn_div_2());
}
#[test]
fn test_conv() {