Skip to content

Commit 8256109

Browse files
committed
Merge #159
159: Add Polygon-Polygon containment predicate and tests r=frewsxcv a=urschrei
2 parents bf13eb5 + cb0e192 commit 8256109

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/algorithm/contains.rs

+24
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ impl<T> Contains<Line<T>> for Polygon<T>
208208
}
209209
}
210210

211+
impl<T> Contains<Polygon<T>> for Polygon<T>
212+
where
213+
T: Float,
214+
{
215+
fn contains(&self, poly: &Polygon<T>) -> bool {
216+
// decompose poly's exterior ring into Lines, and check each for containment
217+
poly.exterior.lines().all(|line| self.contains(&line))
218+
}
219+
}
220+
211221
impl<T> Contains<LineString<T>> for Polygon<T>
212222
where T: Float
213223
{
@@ -243,6 +253,20 @@ impl<T> Contains<Bbox<T>> for Bbox<T>
243253
mod test {
244254
use types::{Coordinate, Point, Line, LineString, Polygon, MultiPolygon, Bbox};
245255
use algorithm::contains::Contains;
256+
#[test]
257+
// V doesn't contain rect because two of its edges intersect with V's exterior boundary
258+
fn polygon_does_not_contain_polygon() {
259+
let v = Polygon::new(vec![(150., 350.), (100., 350.), (210., 160.), (290., 350.), (250., 350.), (200., 250.), (150., 350.)].into(), vec![]);
260+
let rect = Polygon::new(vec![(250., 310.), (150., 310.), (150., 280.), (250., 280.), (250., 310.)].into(), vec![]);
261+
assert_eq!(!v.contains(&rect), true);
262+
}
263+
#[test]
264+
// V contains rect because all its vertices are contained, and none of its edges intersect with V's boundaries
265+
fn polygon_contains_polygon() {
266+
let v = Polygon::new(vec![(150., 350.), (100., 350.), (210., 160.), (290., 350.), (250., 350.), (200., 250.), (150., 350.)].into(), vec![]);
267+
let rect = Polygon::new(vec![(185., 237.), (220., 237.), (220., 220.), (185., 220.), (185., 237.)].into(), vec![]);
268+
assert_eq!(v.contains(&rect), true);
269+
}
246270
/// Tests: Point in LineString
247271
#[test]
248272
fn empty_linestring_test() {

0 commit comments

Comments
 (0)