aboutsummaryrefslogtreecommitdiff
path: root/src/math/polygon
diff options
context:
space:
mode:
authorArne Dußin2020-11-21 20:55:47 +0100
committerArne Dußin2020-11-21 20:55:47 +0100
commitabf55d8d46fc7d5cfccc9f778da6fca10b33d0cd (patch)
treee2f849338a1ce9dfff44082fa7d6b8510385f6f9 /src/math/polygon
parent58ca374fab6dd90c4d7415bdcc98add002274894 (diff)
downloadgraf_karto-abf55d8d46fc7d5cfccc9f778da6fca10b33d0cd.tar.gz
graf_karto-abf55d8d46fc7d5cfccc9f778da6fca10b33d0cd.zip
Move containment of points/ lines into trait
Diffstat (limited to 'src/math/polygon')
-rw-r--r--src/math/polygon/mod.rs53
1 files changed, 30 insertions, 23 deletions
diff --git a/src/math/polygon/mod.rs b/src/math/polygon/mod.rs
index 4530857..d351ec7 100644
--- a/src/math/polygon/mod.rs
+++ b/src/math/polygon/mod.rs
@@ -6,7 +6,7 @@ pub mod triangulate;
pub use polygon_graph::*;
pub use triangulate::*;
-use super::Vec2;
+use super::{LineSegment, Surface, Vec2};
use nalgebra::{ClosedDiv, ClosedMul, ClosedSub, RealField, Scalar};
use num_traits::Zero;
use std::ops::Neg;
@@ -24,10 +24,27 @@ impl<T: Scalar + Copy> Polygon<T> {
/// Check whether a point is inside a polygon or not. If a point lies on an edge, it also
/// counts as inside the polygon.
- pub fn contains_point(&self, p: Vec2<T>) -> bool
+
+ /// Join this polygon with another, ensuring the area of the two stays the same, but the
+ /// overlap is not doubled, but instead joined into one.
+ /// Returns the Polygons themselves, if there is no overlap
+ pub fn unite(self, other: Polygon<T>) -> Vec<Polygon<T>>
where
- T: Zero + ClosedSub + ClosedMul + ClosedDiv + Neg<Output = T> + PartialOrd,
+ T: RealField,
{
+ let mut graph = PolygonGraph::from_polygon(&self);
+ graph.add_all(&other);
+
+ // TODO: Make bounding box support multiple polygons
+ vec![graph.bounding_polygon()]
+ }
+}
+
+impl<
+ T: Scalar + Copy + ClosedSub + ClosedMul + ClosedDiv + Neg<Output = T> + PartialOrd + Zero,
+ > Surface<T> for Polygon<T>
+{
+ fn contains_point(&self, p: &Vec2<T>) -> bool {
let n = self.corners.len();
let a = self.corners[n - 1];
@@ -102,18 +119,8 @@ impl<T: Scalar + Copy> Polygon<T> {
(depth & 1) == 1
}
- /// Join this polygon with another, ensuring the area of the two stays the same, but the
- /// overlap is not doubled, but instead joined into one.
- /// Returns the Polygons themselves, if there is no overlap
- pub fn unite(self, other: Polygon<T>) -> Vec<Polygon<T>>
- where
- T: RealField,
- {
- let mut graph = PolygonGraph::from_polygon(&self);
- graph.add_all(&other);
-
- // TODO: Make bounding box support multiple polygons
- vec![graph.bounding_polygon()]
+ fn contains_line_segment(&self, line_segment: &LineSegment<T>) -> bool {
+ unimplemented!()
}
}
@@ -133,14 +140,14 @@ mod test {
Vec2::new(1., 1.),
]);
- assert!(!polygon.contains_point(Vec2::new(1., -2.)));
- assert!(!polygon.contains_point(Vec2::new(-1., 0.5)));
- assert!(polygon.contains_point(Vec2::new(0., 0.5)));
- assert!(polygon.contains_point(Vec2::new(0.5, 1.)));
- assert!(polygon.contains_point(Vec2::new(0.5, 1.5)));
- assert!(!polygon.contains_point(Vec2::new(-2., 1.9)));
- assert!(!polygon.contains_point(Vec2::new(0., 3.)));
- assert!(polygon.contains_point(Vec2::new(1., 3.)));
+ assert!(!polygon.contains_point(&Vec2::new(1., -2.)));
+ assert!(!polygon.contains_point(&Vec2::new(-1., 0.5)));
+ assert!(polygon.contains_point(&Vec2::new(0., 0.5)));
+ assert!(polygon.contains_point(&Vec2::new(0.5, 1.)));
+ assert!(polygon.contains_point(&Vec2::new(0.5, 1.5)));
+ assert!(!polygon.contains_point(&Vec2::new(-2., 1.9)));
+ assert!(!polygon.contains_point(&Vec2::new(0., 3.)));
+ assert!(polygon.contains_point(&Vec2::new(1., 3.)));
}
#[test]