From 7d559c09e25f4af8bd01fc47e9c02cc075f1ed30 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 30 Nov 2012 22:28:58 +1000 Subject: [PATCH] Add opposite method --- src/angle.rs | 17 +++++++++++++++-- src/test/test_angle.rs | 18 ++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/angle.rs b/src/angle.rs index 24e599b..d1c97ea 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -26,6 +26,7 @@ pub trait Angle: Add pure fn to_radians(&self) -> Radians; pure fn to_degrees(&self) -> Degrees; pure fn wrap(&self) -> self; + pure fn opposite(&self) -> self; } @@ -44,9 +45,15 @@ pub impl Radians: Angle { #[inline(always)] pure fn to_radians(&self) -> Radians { *self } #[inline(always)] pure fn to_degrees(&self) -> Degrees { Degrees(**self * cast(180.0 / pi)) } - #[inline(always)] pure fn wrap(&self) -> Radians { + #[inline(always)] + pure fn wrap(&self) -> Radians { (*self) % cast(2.0 * pi) // TODO: keep in the domain of 0 to two_pi } + + #[inline(always)] + pure fn opposite(&self) -> Radians { + (self + Angle::half_turn()).wrap() // TODO: test! + } } pub impl Radians: Add, Radians> { @@ -123,9 +130,15 @@ pub impl Degrees: Angle { #[inline(always)] pure fn to_radians(&self) -> Radians { Radians(**self * cast(pi / 180.0)) } #[inline(always)] pure fn to_degrees(&self) -> Degrees { *self } - #[inline(always)] pure fn wrap(&self) -> Degrees { + #[inline(always)] + pure fn wrap(&self) -> Degrees { (*self) % cast(360) // TODO: keep in the domain of 0 to 360 } + + #[inline(always)] + pure fn opposite(&self) -> Degrees { + (self + Angle::half_turn()).wrap() // TODO: test! + } } pub impl Degrees: Add, Degrees> { diff --git a/src/test/test_angle.rs b/src/test/test_angle.rs index 918b247..a2750b9 100644 --- a/src/test/test_angle.rs +++ b/src/test/test_angle.rs @@ -21,10 +21,15 @@ fn test_radians() { assert *Radians(pi).wrap() == *Radians(pi); assert *Radians(3.0 * pi).wrap() == *Radians(pi); assert *Radians(2.0 * pi).wrap() == *Radians(0.0); - assert *Radians(-pi).wrap() == *Radians(-pi); - assert *Radians(-3.0 * pi).wrap() == *Radians(-pi); + assert *Radians(-pi).wrap() == *Radians(-pi); // FIXME: should be in the domain of 0 to two_pi + assert *Radians(-3.0 * pi).wrap() == *Radians(-pi); // FIXME: should be in the domain of 0 to two_pi assert *Radians(-2.0 * pi).wrap() == *Radians(0.0); + assert *Radians(0.0).opposite() == *Radians(pi as float); + assert *Radians(pi / 2.0).opposite() == *Radians((pi / 2.0) * 3.0); + assert *Radians(pi * 3.0).opposite() == *Radians(0.0); + assert *Radians(-2.0 * pi).opposite() == *Radians(-pi); // FIXME: should be in the domain of 0 to two_pi + assert *(Radians(pi) + Radians(pi)) == *Radians(2.0 * pi); assert *(Radians(2.0 * pi) - Radians(pi)) == *Radians(pi); assert *(Radians(pi) * 2.0) == *Radians(2.0 * pi); @@ -52,10 +57,15 @@ fn test_degrees() { assert Degrees(90.0).wrap() == Degrees(90.0); assert Degrees(450.0).wrap() == Degrees(90.0); assert Degrees(360.0).wrap() == Degrees(0.0); - assert Degrees(-90.0).wrap() == Degrees(-90.0); - assert Degrees(-450.0).wrap() == Degrees(-90.0); + assert Degrees(-90.0).wrap() == Degrees(-90.0); // FIXME: should be in the domain of 0 to 360 + assert Degrees(-450.0).wrap() == Degrees(-90.0); // FIXME: should be in the domain of 0 to 360 assert Degrees(-360.0).wrap() == Degrees(0.0); + assert Degrees(0.0).opposite() == Degrees(180.0); + assert Degrees(90.0).opposite() == Degrees(270.0); + assert Degrees(540.0).opposite() == Degrees(0.0); + assert Degrees(-360.0).opposite() == Degrees(-180.0); // FIXME: should be in the domain of 0 to 360 + assert (Degrees(180.0) + Degrees(180.0)) == Degrees(360.0); assert (Degrees(360.0) - Degrees(180.0)) == Degrees(180.0); assert (Degrees(360.0) * 2.0) == Degrees(720.0);