From 26a22e0cc7b7cc7a16b6322fd5278a47ade5fcb0 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 29 Nov 2012 11:16:28 +1000 Subject: [PATCH] Implement wrap method --- src/angle.rs | 8 ++++++-- src/test/test_angle.rs | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/angle.rs b/src/angle.rs index 1640907..3487f61 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -37,7 +37,9 @@ pub impl Radians: Angle { #[inline(always)] pure fn to_radians() -> Radians { self } #[inline(always)] pure fn to_degrees() -> Degrees { Degrees(*self * cast(180.0 / pi)) } - #[inline(always)] pure fn wrap() -> Radians { fail(~"Radians.wrap() not yet implemented") } + #[inline(always)] pure fn wrap() -> Radians { + self % cast(2.0 * pi) // TODO: keep in the domain of 0 to two_pi + } } pub impl Radians: Add, Radians> { @@ -87,7 +89,9 @@ pub impl Degrees: Angle { #[inline(always)] pure fn to_radians() -> Radians { Radians(*self * cast(pi / 180.0)) } #[inline(always)] pure fn to_degrees() -> Degrees { self } - #[inline(always)] pure fn wrap() -> Degrees { fail(~"Degrees.wrap() not yet implemented") } + #[inline(always)] pure fn wrap() -> Degrees { + self % cast(360) // TODO: keep in the domain of 0 to 360 + } } pub impl Degrees: Add, Degrees> { diff --git a/src/test/test_angle.rs b/src/test/test_angle.rs index 14c5c63..86792de 100644 --- a/src/test/test_angle.rs +++ b/src/test/test_angle.rs @@ -18,6 +18,13 @@ fn test_radians() { assert *Radians(5.0 * pi).to_radians() == *Radians(5.0 * pi); assert *Radians(-pi).to_radians() == *Radians(-pi); + 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(-2.0 * pi).wrap() == *Radians(0.0); + 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); @@ -40,6 +47,13 @@ fn test_degrees() { assert *Degrees(900.0).to_degrees() == *Degrees(900.0); assert *Degrees(-180.0).to_degrees() == *Degrees(-180.0); + 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(-360.0).wrap() == *Degrees(0.0); + 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);