X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcommon%2Fgeometry.rs;h=d767d8ec4e6ba6fd8941ed287fc6fff9e57713fc;hb=4074267844733949556af129550dfc42fc81da76;hp=5a115fdf63c78049b9cd1b4264bb1ff6a2343799;hpb=8012f86b052f550fe6644ec33a2713f381fc4347;p=kaka%2Frust-sdl-test.git diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 5a115fd..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 { @@ -44,7 +40,7 @@ impl Point { } } -macro_rules! point_op { +macro_rules! impl_point_op { ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => { impl> $trait<$Rhs> for Point { type Output = Self; @@ -68,14 +64,16 @@ macro_rules! point_op { } } -point_op!(+, Add(add), AddAssign(add_assign), rhs = Point => rhs.x, rhs.y); -point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point => rhs.x, rhs.y); -point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point => rhs.x, rhs.y); -point_op!(/, Div(div), DivAssign(div_assign), rhs = Point => rhs.x, rhs.y); -point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1); -point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1); -point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1); -point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1); +impl_point_op!(+, Add(add), AddAssign(add_assign), rhs = Point => rhs.x, rhs.y); +impl_point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point => rhs.x, rhs.y); +impl_point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point => rhs.x, rhs.y); +impl_point_op!(/, Div(div), DivAssign(div_assign), rhs = Point => rhs.x, rhs.y); +impl_point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1); +impl_point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1); +impl_point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1); +impl_point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1); +impl_point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Dimension => rhs.width, rhs.height); +impl_point_op!(/, Div(div), DivAssign(div_assign), rhs = Dimension => rhs.width, rhs.height); ////////// multiply point with scalar ////////////////////////////////////////// impl + Copy> Mul for Point { @@ -145,41 +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 Angle(pub f64); + +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; + } + } + )* } } -#[derive(Debug, Default, PartialEq, Clone, Copy)] -pub struct Degrees(pub f64); -#[derive(Debug, Default, PartialEq, Clone, Copy)] -pub struct Radians(pub f64); +impl_angle!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize); -impl Degrees { - #[allow(dead_code)] - fn to_radians(&self) -> Radians { - Radians(self.0.to_radians()) +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: Angle) -> Angle { + Angle((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU) } } -impl Radians { - #[allow(dead_code)] - fn to_degrees(&self) -> Degrees { - Degrees(self.0.to_degrees()) +// 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; } } @@ -395,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]