From 8d99501918393ea0c046d721e6fa6015fe43b70f Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 18 Nov 2020 21:54:41 +0100 Subject: Implement bounding box function --- src/math/polygon.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src/math/polygon.rs') 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 { pub corners: Vec>, } @@ -96,10 +97,15 @@ impl Polygon { /// 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) -> Vec> { - unimplemented!() + pub fn unite(self, other: Polygon) -> Vec> + 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() -- cgit v1.2.3-70-g09d2