aboutsummaryrefslogtreecommitdiff
path: root/src/math/polygon.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/polygon.rs')
-rw-r--r--src/math/polygon.rs20
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()