X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcommon%2Fgeometry.rs;h=455e600040998e685d04fbd1962a81807195fcf9;hb=60058b918569190f437fe996dfc79daf5a431b91;hp=1cf205eea8758fa4eff764ab46ba244110970d2f;hpb=af18b07f3ff382c0bb122d0e0b235cd7991a2597;p=kaka%2Frust-sdl-test.git diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 1cf205e..455e600 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -1,19 +1,21 @@ use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign, Neg}; +////////// POINT /////////////////////////////////////////////////////////////// + #[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 +36,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 +46,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 +57,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 +68,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 +89,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 +99,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 +110,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 +119,7 @@ impl + Copy> DivAssign for Point2D { } } -impl> Neg for Point2D { +impl> Neg for Point { type Output = Self; fn neg(self) -> Self { @@ -128,34 +130,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(), } @@ -181,45 +183,66 @@ impl Radians { } } +////////// INTERSECTION //////////////////////////////////////////////////////// + +#[derive(Debug)] +pub enum Intersection { + Point(Point), + //Line(Point, Point), // TODO: overlapping collinear + None, +} + +impl Intersection { + pub fn lines(p1: Point, p2: Point, p3: Point, p4: Point) -> Intersection { + let s1 = p2 - p1; + let s2 = p4 - p3; + + let denomimator = -s2.x * s1.y + s1.x * s2.y; + if denomimator != 0.0 { + let s = (-s1.y * (p1.x - p3.x) + s1.x * (p1.y - p3.y)) / denomimator; + let t = ( s2.x * (p1.y - p3.y) - s2.y * (p1.x - p3.x)) / denomimator; + + if s >= 0.0 && s <= 1.0 && t >= 0.0 && t <= 1.0 { + return Intersection::Point(p1 + (s1 * t)) + } + } + + Intersection::None + } +} + +////////// DIMENSION /////////////////////////////////////////////////////////// + #[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, } } } -#[macro_export] -macro_rules! hashmap { - ($($k:expr => $v:expr),*) => { - { - let mut map = std::collections::HashMap::new(); - $(map.insert($k, $v);)* - map - } - } -} +////////// TESTS /////////////////////////////////////////////////////////////// #[cfg(test)] mod tests { @@ -286,14 +309,28 @@ 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 + } + + #[test] + fn intersection_of_lines() { + let p1 = point!(0.0, 0.0); + let p2 = point!(2.0, 2.0); + let p3 = point!(0.0, 2.0); + let p4 = point!(2.0, 0.0); + let r = Intersection::lines(p1, p2, p3, p4); + if let Intersection::Point(p) = r { + assert_eq!(p, point!(1.0, 1.0)); + } else { + panic!(); + } } }