diff options
| author | Arne Dußin | 2020-11-18 21:54:41 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-18 21:54:41 +0100 |
| commit | 8d99501918393ea0c046d721e6fa6015fe43b70f (patch) | |
| tree | 17a15dfb135093163622b61631661e8d4551fdf3 /src/math/polygon.rs | |
| parent | af4f4b91d013384a4af82f48d768ac041662eb20 (diff) | |
| download | graf_karto-8d99501918393ea0c046d721e6fa6015fe43b70f.tar.gz graf_karto-8d99501918393ea0c046d721e6fa6015fe43b70f.zip | |
Implement bounding box function
Diffstat (limited to 'src/math/polygon.rs')
| -rw-r--r-- | src/math/polygon.rs | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/math/polygon.rs b/src/math/polygon.rs index e761136..5711049 100644 --- a/src/math/polygon.rs +++ b/src/math/polygon.rs @@ -1,9 +1,10 @@ -use super::Vec2; -use nalgebra::{ClosedDiv, ClosedMul, ClosedSub, Scalar}; +use super::{PolygonGraph, Vec2}; +use nalgebra::{ClosedDiv, ClosedMul, ClosedSub, RealField, Scalar}; use num_traits::Zero; use std::ops::Neg; #[derive(Debug)] +// TODO: Support polygons with holes pub struct Polygon<T: Scalar + Copy> { pub corners: Vec<Vec2<T>>, } @@ -96,10 +97,15 @@ impl<T: Scalar + Copy> Polygon<T> { /// 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 - // TODO: At the moment, counts holes in the middle as a different polygon. These should be - // attached to the polygon they are inside of. - pub fn unite(self, _other: Polygon<T>) -> Vec<Polygon<T>> { - unimplemented!() + 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()] } } @@ -153,7 +159,7 @@ mod test { println!("Union of the two polygons: {:?}", union); - assert_eq!(union.corners.len(), 10); + assert_eq!(union.corners.len(), 11); assert!(union .corners .iter() |
