@@ -208,6 +208,16 @@ impl<T> Contains<Line<T>> for Polygon<T>
208
208
}
209
209
}
210
210
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
+
211
221
impl < T > Contains < LineString < T > > for Polygon < T >
212
222
where T : Float
213
223
{
@@ -243,6 +253,20 @@ impl<T> Contains<Bbox<T>> for Bbox<T>
243
253
mod test {
244
254
use types:: { Coordinate , Point , Line , LineString , Polygon , MultiPolygon , Bbox } ;
245
255
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
+ }
246
270
/// Tests: Point in LineString
247
271
#[ test]
248
272
fn empty_linestring_test ( ) {
0 commit comments