aboutsummaryrefslogtreecommitdiff
path: root/src/math/polygon/mod.rs
diff options
context:
space:
mode:
authorArne Dußin2020-11-26 20:52:44 +0100
committerArne Dußin2020-11-26 20:52:44 +0100
commit99e935b63bb023cfd46c8f3d81074d3faf7ce592 (patch)
tree5d9739802114cc30065230ce8d425894ae878a5f /src/math/polygon/mod.rs
parent19a1221c4bb9df34bb0c14746fc5372d07d1c771 (diff)
parentcf3c8378557457363853d6795e4ddf9e70a4738e (diff)
downloadgraf_karto-99e935b63bb023cfd46c8f3d81074d3faf7ce592.tar.gz
graf_karto-99e935b63bb023cfd46c8f3d81074d3faf7ce592.zip
Merge branch 'polygon-deletion'
Diffstat (limited to 'src/math/polygon/mod.rs')
-rw-r--r--src/math/polygon/mod.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/math/polygon/mod.rs b/src/math/polygon/mod.rs
index ed48751..c9dad91 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::{LineSegment, Surface, TripletOrientation, Vec2};
+use super::{LineSegment, Rect, Surface, TripletOrientation, Vec2};
use crate::math;
use nalgebra::{ClosedDiv, ClosedMul, ClosedSub, RealField, Scalar};
use num_traits::Zero;
@@ -361,6 +361,54 @@ impl<
true
}
+
+ fn contains_rect(&self, rect: &Rect<T>) -> bool {
+ /* Turn the rectangle into a vector with its hull line segments. If all hull segments are
+ * contained in the polygon, the rectangle is contained completely.
+ */
+ let hull_edges = [
+ // Top left to bottom left.
+ LineSegment::new(
+ Vec2::new(rect.x, rect.y),
+ Vec2::new(rect.x, rect.y + rect.h),
+ ),
+ // Bottom left to bottom right.
+ LineSegment::new(
+ Vec2::new(rect.x, rect.y + rect.h),
+ Vec2::new(rect.x + rect.w, rect.y + rect.h),
+ ),
+ // Bottom right to top right.
+ LineSegment::new(
+ Vec2::new(rect.x + rect.w, rect.y + rect.h),
+ Vec2::new(rect.x + rect.w, rect.y),
+ ),
+ // Top right to top left.
+ LineSegment::new(
+ Vec2::new(rect.x + rect.w, rect.y),
+ Vec2::new(rect.x, rect.y),
+ ),
+ ];
+
+ hull_edges
+ .iter()
+ .all(|edge| self.contains_line_segment(edge))
+ }
+
+ fn contains_polygon(&self, polygon: &Polygon<T>) -> bool {
+ /* Check for all edges of the polygon that they are contained by the polygon. If they all
+ * are, then the polygon itself must also be contained.
+ */
+ for i in 0..polygon.corners.len() {
+ let next = (i + 1) % polygon.corners.len();
+ if !self
+ .contains_line_segment(&LineSegment::new(polygon.corners[i], polygon.corners[next]))
+ {
+ return false;
+ }
+ }
+
+ true
+ }
}
/* Helper function to calculate the combined angle of a set of points when connecting them one