X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcommon%2Fgeometry.rs;h=d767d8ec4e6ba6fd8941ed287fc6fff9e57713fc;hb=4074267844733949556af129550dfc42fc81da76;hp=3fee2adb1cc0a2e1c9683141683e1a80fb9bfec6;hpb=0d75b79ec38b321835c2f3984252f27ff271f8b4;p=kaka%2Frust-sdl-test.git diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 3fee2ad..d767d8e 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -28,12 +28,8 @@ impl Point { } } - pub fn to_radians(&self) -> Radians { - Radians(self.y.atan2(self.x)) - } - - pub fn to_degrees(&self) -> Degrees { - self.to_radians().to_degrees() + pub fn to_angle(&self) -> Angle { + self.y.atan2(self.x).radians() } pub fn to_i32(self) -> Point { @@ -147,46 +143,115 @@ impl From> for (T, T) { } } -impl From for Point { - fn from(item: Degrees) -> Self { - let r = item.0.to_radians(); - Point { - x: r.cos(), - y: r.sin(), - } +impl From for Point { + fn from(item: Angle) -> Self { + Point { + x: item.0.cos(), + y: item.0.sin(), + } } } -impl From for Point { - fn from(item: Radians) -> Self { - Point { - x: item.0.cos(), - y: item.0.sin(), - } - } -} +////////// ANGLE /////////////////////////////////////////////////////////////// #[derive(Debug, Default, PartialEq, Clone, Copy)] -pub struct Degrees(pub f64); -#[derive(Debug, Default, PartialEq, Clone, Copy)] -pub struct Radians(pub f64); +pub struct Angle(pub f64); -impl Degrees { - #[allow(dead_code)] - pub fn to_radians(&self) -> Radians { - Radians(self.0.to_radians()) +pub trait ToAngle { + fn radians(self) -> Angle; + fn degrees(self) -> Angle; +} + +macro_rules! impl_angle { + ($($type:ty),*) => { + $( + impl ToAngle for $type { + fn radians(self) -> Angle { + Angle(self as f64) + } + + fn degrees(self) -> Angle { + Angle((self as f64).to_radians()) + } + } + + impl Mul<$type> for Angle { + type Output = Self; + + fn mul(self, rhs: $type) -> Self { + Angle(self.0 * (rhs as f64)) + } + } + + impl MulAssign<$type> for Angle { + fn mul_assign(&mut self, rhs: $type) { + self.0 *= rhs as f64; + } + } + + impl Div<$type> for Angle { + type Output = Self; + + fn div(self, rhs: $type) -> Self { + Angle(self.0 / (rhs as f64)) + } + } + + impl DivAssign<$type> for Angle { + fn div_assign(&mut self, rhs: $type) { + self.0 /= rhs as f64; + } + } + )* } } -impl Radians { - #[allow(dead_code)] - pub fn to_degrees(&self) -> Degrees { - Degrees(self.0.to_degrees()) +impl_angle!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize); + +impl Angle { + pub fn to_radians(self) -> f64 { + self.0 + } + + pub fn to_degrees(self) -> f64 { + self.0.to_degrees() } /// Returns the reflection of the incident when mirrored along this angle. - pub fn mirror(&self, incidence: Radians) -> Radians { - Radians((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU) + pub fn mirror(&self, incidence: Angle) -> Angle { + Angle((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU) + } +} + +// TODO override eq, 0==360 osv + +// addition and subtraction of angles + +impl Add for Angle { + type Output = Self; + + fn add(self, rhs: Angle) -> Self { + Angle(self.0 + rhs.0) + } +} + +impl AddAssign for Angle { + fn add_assign(&mut self, rhs: Angle) { + self.0 += rhs.0; + } +} + +impl Sub for Angle { + type Output = Self; + + fn sub(self, rhs: Angle) -> Self { + Angle(self.0 - rhs.0) + } +} + +impl SubAssign for Angle { + fn sub_assign(&mut self, rhs: Angle) { + self.0 -= rhs.0; } } @@ -402,11 +467,11 @@ mod tests { #[test] fn angles() { - assert_eq!(Radians(0.0).to_degrees(), Degrees(0.0)); - assert_eq!(Radians(std::f64::consts::PI).to_degrees(), Degrees(180.0)); - assert_eq!(Degrees(180.0).to_radians(), Radians(std::f64::consts::PI)); - assert!((Point::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001); - assert!((Point::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001); + assert_eq!(0.radians(), 0.degrees()); + assert_eq!(180.degrees(), std::f64::consts::PI.radians()); + assert_eq!(std::f64::consts::PI.radians().to_degrees(), 180.0); + assert!((Point::from(90.degrees()) - point!(0.0, 1.0)).length() < 0.001); + assert!((Point::from(std::f64::consts::FRAC_PI_2.radians()) - point!(0.0, 1.0)).length() < 0.001); } #[test]