@@ -488,11 +488,11 @@ def test_find_intersection_overlapping(self):
488
488
489
489
touching , mtv = axisall .AxisAlignedLine .find_intersection (_aal1 , _aal2 )
490
490
self .assertFalse (touching )
491
- self .assertEquals (- 1 , mtv )
491
+ self .assertEqual (- 1 , mtv )
492
492
493
493
touching , mtv = axisall .AxisAlignedLine .find_intersection (_aal2 , _aal1 )
494
494
self .assertFalse (touching )
495
- self .assertEquals (1 , mtv )
495
+ self .assertEqual (1 , mtv )
496
496
497
497
def test_contains_point_false (self ):
498
498
_aal1 = axisall .AxisAlignedLine (self .vec_1_1 , 0 , 1 )
@@ -526,7 +526,184 @@ def test_contains_point_inner(self):
526
526
outer , inner = axisall .AxisAlignedLine .contains_point (_aal1 , 0.75 )
527
527
self .assertFalse (outer )
528
528
self .assertTrue (inner )
529
+
530
+ class TestPolygon (unittest .TestCase ):
531
+ def setUp (self ):
532
+ random .seed ()
533
+
534
+ def test_constructor_standard (self ):
535
+ poly = polygon2 .Polygon2 ([ vector2 .Vector2 (0 , 1 ),
536
+ vector2 .Vector2 (1 , 1 ),
537
+ vector2 .Vector2 (1 , 0 ),
538
+ vectro2 .Vector2 (0 , 0 ) ])
539
+
540
+ self .assertEqual (4 , len (poly .points ))
541
+ self .assertEqual (4 , len (poly .lines ))
542
+ self .assertEqual (2 , len (poly .normals ))
543
+
544
+ self .assertEqual (0 , poly .points [0 ].x )
545
+ self .assertEqual (1 , poly .points [0 ].y )
546
+ self .assertEqual (1 , poly .points [1 ].x )
547
+ self .assertEqual (1 , poly .points [1 ].y )
548
+ self .assertEqual (1 , poly .points [2 ].x )
549
+ self .assertEqual (0 , poly .points [2 ].y )
550
+ self .assertEqual (0 , poly .points [3 ].x )
551
+ self .assertEqual (0 , poly .points [3 ].y )
552
+
553
+ self .assertEqual (0 , poly .lines [0 ].start .x )
554
+ self .assertEqual (1 , poly .lines [0 ].start .y )
555
+ self .assertEqual (1 , poly .lines [0 ].end .x )
556
+ self .assertEqual (1 , poly .lines [0 ].end .y )
557
+ self .assertEqual (1 , poly .lines [1 ].start .x )
558
+ self .assertEqual (1 , poly .lines [1 ].start .y )
559
+ self .assertEqual (1 , poly .lines [1 ].end .x )
560
+ self .assertEqual (0 , poly .lines [1 ].end .y )
561
+ self .assertEqual (1 , poly .lines [2 ].start .x )
562
+ self .assertEqual (0 , poly .lines [2 ].start .y )
563
+ self .assertEqual (0 , poly .lines [2 ].end .x )
564
+ self .assertEqual (0 , poly .lines [2 ].end .y )
565
+ self .assertEqual (0 , poly .lines [3 ].start .x )
566
+ self .assertEqual (0 , poly .lines [3 ].start .y )
567
+ self .assertEqual (0 , poly .lines [3 ].end .x )
568
+ self .assertEqual (1 , poly .lines [3 ].end .y )
569
+
570
+ self .assertIsNotNone (next (vec for vec in poly .normals if vec .horizontal , None ))
571
+ self .assertIsNotNone (next (vec for vec in poly .normals if vec .vertical , None ))
572
+
573
+ self .assertAlmostEqual (0.5 , poly .center .x )
574
+ self .assertAlmostEqual (0.5 , poly .center .y )
575
+
576
+ poly2 = polygon2 .Polygon2 ([ (0 , 1 ), (1 , 1 ), (1 , 0 ), (0 , 0 ) ])
577
+
578
+ self .assertEqual (4 , len (poly2 .points ))
579
+ self .assertEqual (4 , len (poly2 .lines ))
580
+ self .assertEqual (2 , len (poly2 .normals ))
581
+
582
+ with self .assertRaises (StopIteration ):
583
+ next (i for i in range (4 ) if poly .points [i ].x != poly2 .points [i ].x or poly .points [i ].y != poly2 .points [i ].y )
584
+
585
+
586
+ def test_from_regular (self ):
587
+ diamond = polygon2 .Polygon2 .from_regular (4 , 1 )
588
+
589
+ self .assertEqual (2 , diamond .points [0 ].x )
590
+ self .assertEqual (1 , diamond .points [0 ].y )
591
+ self .assertEqual (1 , diamond .points [1 ].x )
592
+ self .assertEqual (0 , diamond .points [1 ].y )
593
+ self .assertEqual (0 , diamond .points [2 ].x )
594
+ self .assertEqual (1 , diamond .points [2 ].y )
595
+ self .assertEqual (1 , diamond .points [3 ].x )
596
+ self .assertEqual (2 , diamond .points [3 ].y )
597
+
598
+ diamond_shifted = polygon2 .Polygon2 .from_regular (4 , 1 , center = vector2 .Vector2 (0 , 0 ))
599
+
600
+ with self .assertRaises (StopIteration ):
601
+ next (i for i in range (4 ) if diamond .points [i ].x != diamond_shifted .points [i ].x + 1 or diamond .points [i ].y != diamond_shifted .points [i ].y + 1 )
602
+
603
+ square = polygon2 .Polygon2 .from_regular (4 , 1 , math .pi / 4 )
604
+
605
+ self .assertEqual (0 , poly .points [0 ].x )
606
+ self .assertEqual (1 , poly .points [0 ].y )
607
+ self .assertEqual (1 , poly .points [1 ].x )
608
+ self .assertEqual (1 , poly .points [1 ].y )
609
+ self .assertEqual (1 , poly .points [2 ].x )
610
+ self .assertEqual (0 , poly .points [2 ].y )
611
+ self .assertEqual (0 , poly .points [3 ].x )
612
+ self .assertEqual (0 , poly .points [3 ].y )
613
+
614
+ square2 = polygon2 .Polygon2 .from_regular (4 , 1 , start_degs = 45 )
615
+
616
+ with self .assertRaises (StopIteration ):
617
+ next (i for i in range (4 ) if square .points [i ].x != square2 .points [i ].x or square .points [i ].y != square2 .points [i ].y )
618
+
619
+
620
+ def test_from_rotated (self ):
621
+ # isos triangle
622
+ # weighted total = (0 + 1 + 2, 0 + 1 + 1) = (3, 2)
623
+ # center = (1, 2/3)
624
+ triangle = polygon2 .Polygon2 ([ (0 , 0 ), (1 , 1 ), (2 , 1 ) ])
625
+
626
+ triangle_rot = polygon2 .Polygon2 .from_rotated (triangle , math .pi / 4 )
627
+
628
+ # example of how to calculate:
629
+ # shift so you rotate about origin (subtract center)
630
+ # (0, 0) - (1, 2/3) = (-1, -2/3)
631
+ # rotate 45 degrees clockwise = (-1 * cos(45) - (-2/3) * sin(45), (-2/3) * cos(45) + (-1) * sin(45)) = (-0.23570226039, -1.17851130198)
632
+ # shift back (add center): (0.76429773961, -0.51184463531)
633
+ self .assertAlmostEqual (0.76429773961 , triangle_rot .points [0 ].x )
634
+ self .assertAlmostEqual (- 0.51184463531 , triangle_rot .points [0 ].y )
635
+ self .assertAlmostEqual (1.23570226039 , triangle_rot .points [1 ].x )
636
+ self .assertAlmostEqual (0.90236892706 , triangle_rot .points [1 ].y )
637
+ self .assertAlmostEqual (0.47140452079 , triangle_rot .points [2 ].x )
638
+ self .assertAlmostEqual (1.60947570825 , triangle_rot .points [2 ].y )
639
+ self .assertAlmostEqual (1 , triangle_rot .center .x )
640
+ self .assertAlmostEqual (0.66666666667 , triangle_rot .center .y )
641
+
642
+
643
+ def test_area (self ):
644
+ # http://www.mathopenref.com/coordpolygonareacalc.html# helpful for checking
645
+ poly = polygon2 .Polygon2 .from_regular (4 , 1 )
646
+ self .assertAlmostEqual (1 , poly .area )
647
+
648
+ poly2 = polygon2 .Polygon2 .from_regular (4 , 2 )
649
+ self .assertAlmostEqual (4 , poly2 .area )
650
+
651
+ poly3 = polygon2 .Polygon2 .from_regular (8 , 3.7 )
652
+ self .assertAlmostEqual (38.7 , poly3 .area )
653
+
654
+ poly4 = polygon2 .Polygon2 ([ (0 , 0 ), (1 , 1 ), (2 , 1 ) ])
655
+ self .assertAlmostEqual (0.5 , poly4 .area )
656
+
657
+ poly5 = polygon2 .Polygon2 ([ (0 , 0 ), (1 , 1 ), (2 , 1 ), (1 , - 0.25 ) ])
658
+ self .assertAlmostEqual (1.25 , poly5 .area )
659
+
660
+ def _proj_onto_axis_fuzzer (self , points , axis , expected ):
661
+ for i in range (3 ):
662
+ offset = vector2 .Vector2 (random .randrange (- 1000 , 1000 , 0.01 ), random .randrange (- 1000 , 1000 , 0.01 ))
663
+
664
+ new_points = []
665
+ for pt in points :
666
+ new_points .append (pt - offset )
667
+
668
+ new_poly = polygon2 .Polygon2 (new_points )
669
+
670
+ proj = polygon2 .Polygon2 .project_onto_axis (new_poly , offset , axis )
671
+
672
+ help_msg = "points={}, axis={}, expected={} proj={} [offset = {}, new_points={}]" .format (points , axis , expected , proj , offset , new_points )
673
+ self .assertAlmostEqual (expected .min , proj .min , help_msg )
674
+ self .assertAlmostEqual (expected .max , proj .max , help_msg )
675
+
676
+
677
+ def test_project_onto_axis (self ):
678
+ poly = polygon2 .Polygon2 .from_regular (4 , 1 , math .pi / 4 )
529
679
680
+ _axis = vector2 .Vector2 (0 , 1 )
681
+ self ._proj_onto_axis_fuzzer (poly .points , _axis , axisall .AxisAlignedLine (_axis , 0 , 1 ))
682
+
683
+ _axis2 = vector2 .Vector2 (1 , 0 )
684
+ self ._proj_onto_axis_fuzzer (poly .points , _axis2 , axisall .AxisAlignedLine (_axis2 , 0 , 1 ))
685
+
686
+ _axis3 = vector2 .Vector2 (0.70710678118 , 0.70710678118 )
687
+ self ._proj_onto_axis_fuzzer (poly .points , _axis3 , axisall .AxisAlignedLine (_axis3 , 0 , 1.41421356236 ))
688
+
689
+
690
+ def test_contains_point_false (self ):
691
+ pass
692
+
693
+ def test_contains_point_edge (self ):
694
+ pass
695
+
696
+ def test_contains_point_contained (self ):
697
+ pass
698
+
699
+ def test_find_intersection_false (self ):
700
+ pass
701
+
702
+ def test_find_intersection_touching (self ):
703
+ pass
704
+
705
+ def test_find_intersection_overlapping (self ):
706
+ pass
530
707
531
708
if __name__ == '__main__' :
532
709
unittest .main ()
0 commit comments