@@ -95,6 +95,7 @@ pub(crate) use impl_contains_geometry_for;
95
95
mod test {
96
96
use crate :: line_string;
97
97
use crate :: Contains ;
98
+ use crate :: Relate ;
98
99
use crate :: { coord, Coord , Line , LineString , MultiPolygon , Point , Polygon , Rect , Triangle } ;
99
100
100
101
#[ test]
@@ -555,4 +556,186 @@ mod test {
555
556
let pt: Point = ( 0.5 , 0.5 ) . into ( ) ;
556
557
assert ! ( !tri. contains( & pt) ) ;
557
558
}
559
+
560
+ #[ test]
561
+ fn rect_contains_polygon ( ) {
562
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
563
+ let poly = Polygon :: new (
564
+ line_string ! [
565
+ ( x: 150. , y: 350. ) ,
566
+ ( x: 100. , y: 350. ) ,
567
+ ( x: 210. , y: 160. ) ,
568
+ ( x: 290. , y: 350. ) ,
569
+ ( x: 250. , y: 350. ) ,
570
+ ( x: 200. , y: 250. ) ,
571
+ ( x: 150. , y: 350. ) ,
572
+ ] ,
573
+ vec ! [ ] ,
574
+ ) ;
575
+ assert_eq ! ( rect. contains( & poly) , rect. relate( & poly) . is_contains( ) ) ;
576
+ }
577
+
578
+ #[ test]
579
+ fn rect_contains_touching_polygon ( ) {
580
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
581
+ let touching_poly = Polygon :: new (
582
+ line_string ! [
583
+ ( x: 150. , y: 350. ) ,
584
+ ( x: 90. , y: 350. ) ,
585
+ ( x: 210. , y: 160. ) ,
586
+ ( x: 290. , y: 350. ) ,
587
+ ( x: 250. , y: 350. ) ,
588
+ ( x: 200. , y: 250. ) ,
589
+ ( x: 150. , y: 350. ) ,
590
+ ] ,
591
+ vec ! [ ] ,
592
+ ) ;
593
+ assert_eq ! (
594
+ rect. contains( & touching_poly) ,
595
+ rect. relate( & touching_poly) . is_contains( )
596
+ ) ;
597
+
598
+ let touching_rect = Rect :: new ( coord ! { x: 90. , y: 200. } , coord ! { x: 200. , y: 300. } ) ;
599
+ assert_eq ! (
600
+ rect. contains( & touching_rect) ,
601
+ rect. relate( & touching_rect) . is_contains( )
602
+ ) ;
603
+ }
604
+
605
+ #[ test]
606
+ fn rect_contains_empty_polygon ( ) {
607
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
608
+ let empty_poly = Polygon :: new ( line_string ! [ ] , vec ! [ ] ) ;
609
+ assert_eq ! (
610
+ rect. contains( & empty_poly) ,
611
+ rect. relate( & empty_poly) . is_contains( )
612
+ ) ;
613
+ }
614
+
615
+ #[ test]
616
+ fn rect_contains_polygon_empty_area ( ) {
617
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
618
+ let empty_poly = Polygon :: new (
619
+ line_string ! [
620
+ ( x: 100. , y: 200. ) ,
621
+ ( x: 100. , y: 200. ) ,
622
+ ( x: 100. , y: 200. ) ,
623
+ ( x: 100. , y: 200. ) ,
624
+ ] ,
625
+ vec ! [ ] ,
626
+ ) ;
627
+ assert_eq ! (
628
+ rect. contains( & empty_poly) ,
629
+ rect. relate( & empty_poly) . is_contains( )
630
+ ) ;
631
+ }
632
+
633
+ #[ test]
634
+ fn rect_contains_rect_polygon ( ) {
635
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
636
+ let rect_poly = rect. to_polygon ( ) ;
637
+ assert_eq ! (
638
+ rect. contains( & rect_poly) ,
639
+ rect. relate( & rect_poly) . is_contains( )
640
+ ) ;
641
+ }
642
+
643
+ #[ test]
644
+ fn rect_contains_polygon_in_boundary ( ) {
645
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
646
+ let poly_one_border =
647
+ Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 90. , y: 360. } ) . to_polygon ( ) ;
648
+ assert_eq ! (
649
+ rect. contains( & poly_one_border) ,
650
+ rect. relate( & poly_one_border) . is_contains( )
651
+ ) ;
652
+
653
+ let poly_two_borders = Polygon :: new (
654
+ line_string ! [
655
+ ( x: 90. , y: 150. ) ,
656
+ ( x: 300. , y: 150. ) ,
657
+ ( x: 90. , y: 150. ) ,
658
+ ( x: 90. , y: 360. ) ,
659
+ ( x: 90. , y: 150. ) ,
660
+ ] ,
661
+ vec ! [ ] ,
662
+ ) ;
663
+ assert_eq ! (
664
+ rect. contains( & poly_two_borders) ,
665
+ rect. relate( & poly_two_borders) . is_contains( )
666
+ ) ;
667
+
668
+ let poly_two_borders_triangle = Polygon :: new (
669
+ line_string ! [
670
+ ( x: 90. , y: 150. ) ,
671
+ ( x: 300. , y: 150. ) ,
672
+ ( x: 90. , y: 360. ) ,
673
+ ( x: 90. , y: 150. ) ,
674
+ ] ,
675
+ vec ! [ ] ,
676
+ ) ;
677
+ assert_eq ! (
678
+ rect. contains( & poly_two_borders_triangle) ,
679
+ rect. relate( & poly_two_borders_triangle) . is_contains( )
680
+ ) ;
681
+ }
682
+
683
+ #[ test]
684
+ fn rect_contains_polygon_in_boundary_with_hole ( ) {
685
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
686
+ let poly_two_borders_triangle_with_hole = Polygon :: new (
687
+ line_string ! [
688
+ ( x: 90. , y: 150. ) ,
689
+ ( x: 300. , y: 150. ) ,
690
+ ( x: 90. , y: 360. ) ,
691
+ ( x: 90. , y: 150. ) ,
692
+ ] ,
693
+ vec ! [ line_string![
694
+ ( x: 90. , y: 150. ) ,
695
+ ( x: 300. , y: 150. ) ,
696
+ ( x: 90. , y: 360. ) ,
697
+ ( x: 90. , y: 150. ) ,
698
+ ] ] ,
699
+ ) ;
700
+ assert_eq ! (
701
+ rect. contains( & poly_two_borders_triangle_with_hole) ,
702
+ rect. relate( & poly_two_borders_triangle_with_hole)
703
+ . is_contains( )
704
+ ) ;
705
+ }
706
+
707
+ #[ test]
708
+ fn rect_empty_contains_polygon ( ) {
709
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 90. , y: 150. } ) ;
710
+ let poly = Polygon :: new (
711
+ line_string ! [
712
+ ( x: 150. , y: 350. ) ,
713
+ ( x: 100. , y: 350. ) ,
714
+ ( x: 210. , y: 160. ) ,
715
+ ( x: 290. , y: 350. ) ,
716
+ ( x: 250. , y: 350. ) ,
717
+ ( x: 200. , y: 250. ) ,
718
+ ( x: 150. , y: 350. ) ,
719
+ ] ,
720
+ vec ! [ ] ,
721
+ ) ;
722
+ assert_eq ! ( rect. contains( & poly) , rect. relate( & poly) . is_contains( ) ) ;
723
+
724
+ let rect_poly = rect. to_polygon ( ) ;
725
+ assert_eq ! (
726
+ rect. contains( & rect_poly) ,
727
+ rect. relate( & rect_poly) . is_contains( )
728
+ ) ;
729
+ }
730
+
731
+ #[ test]
732
+ fn rect_contains_point ( ) {
733
+ let rect = Rect :: new ( coord ! { x: 90. , y: 150. } , coord ! { x: 300. , y: 360. } ) ;
734
+
735
+ let point1 = Point :: new ( 100. , 200. ) ;
736
+ assert_eq ! ( rect. contains( & point1) , rect. relate( & point1) . is_contains( ) ) ;
737
+
738
+ let point2 = Point :: new ( 90. , 200. ) ;
739
+ assert_eq ! ( rect. contains( & point2) , rect. relate( & point2) . is_contains( ) ) ;
740
+ }
558
741
}
0 commit comments