Add opposite method

This commit is contained in:
Brendan Zabarauskas 2012-11-30 22:28:58 +10:00
parent f352cd68b4
commit 7d559c09e2
2 changed files with 29 additions and 6 deletions

View file

@ -26,6 +26,7 @@ pub trait Angle<T>: Add<self,self>
pure fn to_radians(&self) -> Radians<T>;
pure fn to_degrees(&self) -> Degrees<T>;
pure fn wrap(&self) -> self;
pure fn opposite(&self) -> self;
}
@ -44,9 +45,15 @@ pub impl<T:Copy Num NumCast> Radians<T>: Angle<T> {
#[inline(always)] pure fn to_radians(&self) -> Radians<T> { *self }
#[inline(always)] pure fn to_degrees(&self) -> Degrees<T> { Degrees(**self * cast(180.0 / pi)) }
#[inline(always)] pure fn wrap(&self) -> Radians<T> {
#[inline(always)]
pure fn wrap(&self) -> Radians<T> {
(*self) % cast(2.0 * pi) // TODO: keep in the domain of 0 to two_pi
}
#[inline(always)]
pure fn opposite(&self) -> Radians<T> {
(self + Angle::half_turn()).wrap() // TODO: test!
}
}
pub impl<T:Copy Num> Radians<T>: Add<Radians<T>, Radians<T>> {
@ -123,9 +130,15 @@ pub impl<T:Copy Num NumCast> Degrees<T>: Angle<T> {
#[inline(always)] pure fn to_radians(&self) -> Radians<T> { Radians(**self * cast(pi / 180.0)) }
#[inline(always)] pure fn to_degrees(&self) -> Degrees<T> { *self }
#[inline(always)] pure fn wrap(&self) -> Degrees<T> {
#[inline(always)]
pure fn wrap(&self) -> Degrees<T> {
(*self) % cast(360) // TODO: keep in the domain of 0 to 360
}
#[inline(always)]
pure fn opposite(&self) -> Degrees<T> {
(self + Angle::half_turn()).wrap() // TODO: test!
}
}
pub impl<T:Copy Num> Degrees<T>: Add<Degrees<T>, Degrees<T>> {

View file

@ -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);