X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcommon%2Fgeometry.rs;h=5a03ffb79da30bf2b36007fe0c920bf88989bd18;hb=1f42d724d84ed1c014ff40ccc91058472391be0c;hp=1cf205eea8758fa4eff764ab46ba244110970d2f;hpb=af18b07f3ff382c0bb122d0e0b235cd7991a2597;p=kaka%2Frust-sdl-test.git diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 1cf205e..5a03ffb 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -3,17 +3,17 @@ use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, N #[macro_export] macro_rules! point { ( $x:expr, $y:expr ) => { - Point2D { x: $x, y: $y } + Point { x: $x, y: $y } }; } #[derive(Debug, Default, Copy, Clone, PartialEq)] -pub struct Point2D { +pub struct Point { pub x: T, pub y: T, } -impl Point2D { +impl Point { pub fn length(&self) -> f64 { ((self.x * self.x) + (self.y * self.y)).sqrt() } @@ -34,8 +34,8 @@ impl Point2D { self.to_radians().to_degrees() } - pub fn to_i32(self) -> Point2D { - Point2D { + pub fn to_i32(self) -> Point { + Point { x: self.x as i32, y: self.y as i32, } @@ -44,7 +44,7 @@ impl Point2D { macro_rules! 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 Point2D { + impl> $trait<$Rhs> for Point { type Output = Self; fn $fn(self, $rhs: $Rhs) -> Self { @@ -55,7 +55,7 @@ macro_rules! point_op { } } - impl + Copy> $trait_assign<$Rhs> for Point2D { + impl + Copy> $trait_assign<$Rhs> for Point { fn $fn_assign(&mut self, $rhs: $Rhs) { *self = Self { x: self.x $op $x, @@ -66,17 +66,17 @@ macro_rules! point_op { } } -point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D => rhs.x, rhs.y); -point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D => rhs.x, rhs.y); -point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D => rhs.x, rhs.y); -point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D => rhs.x, rhs.y); +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); ////////// multiply point with scalar ////////////////////////////////////////// -impl + Copy> Mul for Point2D { +impl + Copy> Mul for Point { type Output = Self; fn mul(self, rhs: T) -> Self { @@ -87,7 +87,7 @@ impl + Copy> Mul for Point2D { } } -impl + Copy> MulAssign for Point2D { +impl + Copy> MulAssign for Point { fn mul_assign(&mut self, rhs: T) { *self = Self { x: self.x * rhs, @@ -97,7 +97,7 @@ impl + Copy> MulAssign for Point2D { } ////////// divide point with scalar //////////////////////////////////////////// -impl + Copy> Div for Point2D { +impl + Copy> Div for Point { type Output = Self; fn div(self, rhs: T) -> Self { @@ -108,7 +108,7 @@ impl + Copy> Div for Point2D { } } -impl + Copy> DivAssign for Point2D { +impl + Copy> DivAssign for Point { fn div_assign(&mut self, rhs: T) { *self = Self { x: self.x / rhs, @@ -117,7 +117,7 @@ impl + Copy> DivAssign for Point2D { } } -impl> Neg for Point2D { +impl> Neg for Point { type Output = Self; fn neg(self) -> Self { @@ -128,34 +128,34 @@ impl> Neg for Point2D { } } -impl From<(T, T)> for Point2D { +impl From<(T, T)> for Point { fn from(item: (T, T)) -> Self { - Point2D { + Point { x: item.0, y: item.1, } } } -impl From> for (T, T) { - fn from(item: Point2D) -> Self { +impl From> for (T, T) { + fn from(item: Point) -> Self { (item.x, item.y) } } -impl From for Point2D { +impl From for Point { fn from(item: Degrees) -> Self { let r = item.0.to_radians(); - Point2D { + Point { x: r.cos(), y: r.sin(), } } } -impl From for Point2D { +impl From for Point { fn from(item: Radians) -> Self { - Point2D { + Point { x: item.0.cos(), y: item.0.sin(), } @@ -182,28 +182,28 @@ impl Radians { } #[macro_export] -macro_rules! rect { - ( $x:expr, $y:expr ) => { - Rect { x: $x, y: $y } +macro_rules! dimen { + ( $w:expr, $h:expr ) => { + Dimension { width: $w, height: $h } }; } -#[derive(Default)] -pub struct Rect { +#[derive(Debug, Default)] +pub struct Dimension { pub width: T, pub height: T, } -impl + Copy> Rect { +impl + Copy> Dimension { #[allow(dead_code)] pub fn area(&self) -> T { self.width * self.height } } -impl From<(T, T)> for Rect { +impl From<(T, T)> for Dimension { fn from(item: (T, T)) -> Self { - Rect { + Dimension { width: item.0, height: item.1, } @@ -286,14 +286,14 @@ mod tests { 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!((Point2D::from(Degrees(90.0)) - point!(0.0, 1.0)).length() < 0.001); - assert!((Point2D::from(Radians(std::f64::consts::FRAC_PI_2)) - point!(0.0, 1.0)).length() < 0.001); + 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); } #[test] - fn area_for_rect_of_multipliable_type() { - let r: Rect<_> = (30, 20).into(); // the Into trait uses the From trait + fn area_for_dimension_of_multipliable_type() { + let r: Dimension<_> = (30, 20).into(); // the Into trait uses the From trait assert_eq!(r.area(), 30 * 20); - // let a = Rect::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String + // let a = Dimension::from(("a".to_string(), "b".to_string())).area(); // this doesn't work, because area() is not implemented for String } }