def test_intersect_polygon_segments_with_3_angled_rectangles(): """Tests that a vertex shared by 2 polygons is added only once to a 3rd polygon Make sure the addedvertex is which is colinear within tolerance. The polygons are rotated 45 degrees counter-clockwise to introduce floating-point closeness considerations. """ r2 = math.sqrt(2.0) tolerance = 0.02 expected_point = Point2D(r2, 0) pts0 = (Point2D(0, 0), Point2D(0.5 * r2 * 0.99, -0.5 * r2 * 0.99), Point2D(1.5 * r2 * 0.99, 0.5 * r2 * 0.99), Point2D(r2, r2)) polygon0 = Polygon2D(pts0) pts1 = (Point2D(0.5 * r2, -0.5 * r2), Point2D(r2, -r2), Point2D(1.5 * r2, -0.5 * r2), expected_point) polygon1 = Polygon2D(pts1) pts2 = (expected_point, Point2D(1.5 * r2, -0.5 * r2), Point2D(2 * r2, 0), Point2D(1.5 * r2, 0.5 * r2)) polygon2 = Polygon2D(pts2) polygons = Polygon2D.intersect_polygon_segments([polygon0, polygon1, polygon2], tolerance) # Extra vertex added to largest polygon, as expected assert len(polygons[0].segments) == 5 assert polygons[0].segments[1].p2 == polygons[0].vertices[2] assert polygons[0].segments[2].p1 == polygons[0].vertices[2] assert len(polygon1.segments) == 4 # No extra vertex added assert len(polygon2.segments) == 4 # No extra vertex added
def test_intersect_polygon_segments_abraham_bug(): """"Test the polygon intersection with the bug found by Abraham.""" pts1 = [Point2D(-154.33, -272.63), Point2D(-155.30, -276.70), Point2D(-151.23, -277.68), Point2D(-150.26, -273.61), Point2D(-146.19, -274.58), Point2D(-145.22, -270.51), Point2D(-149.29, -269.54), Point2D(-148.31, -265.47), Point2D(-152.38, -264.50), Point2D(-153.35, -268.57), Point2D(-157.42, -267.59), Point2D(-158.39, -271.66)] pts2 = [Point2D(-161.49, -266.62), Point2D(-153.35, -268.57), Point2D(-151.41, -260.43), Point2D(-159.54, -258.49)] pts3 = [Point2D(-164.41, -278.82), Point2D(-156.27, -280.77), Point2D(-154.33, -272.63), Point2D(-162.46, -270.69)] pts4 = [Point2D(-149.29, -269.54), Point2D(-141.15, -271.49), Point2D(-139.20, -263.35), Point2D(-147.34, -261.41)] pts5 = [Point2D(-152.21, -281.74), Point2D(-144.07, -283.69), Point2D(-142.12, -275.55), Point2D(-150.26, -273.61)] polygon1 = Polygon2D(pts1) polygon2 = Polygon2D(pts2) polygon3 = Polygon2D(pts3) polygon4 = Polygon2D(pts4) polygon5 = Polygon2D(pts5) polygons = [polygon1, polygon2, polygon3, polygon4, polygon5] new_polygons = Polygon2D.intersect_polygon_segments(polygons, 0.01) for polygon in new_polygons: assert not polygon.is_self_intersecting
def test_intersect_polygon_segments_with_3_rectangles(): """Tests that a vertex shared by 2 polygons is added only once to a 3rd polygon which is colinear.""" pts0 = (Point2D(0, 2), Point2D(4, 2), Point2D(4, 4), Point2D(0, 4)) polygon0 = Polygon2D(pts0) pts1 = (Point2D(0, 0), Point2D(2, 0), Point2D(2, 2), Point2D(0, 2)) polygon1 = Polygon2D(pts1) pts2 = (Point2D(2, 0), Point2D(4, 0), Point2D(4, 2), Point2D(2, 2)) polygon2 = Polygon2D(pts2) polygons = Polygon2D.intersect_polygon_segments([polygon0, polygon1, polygon2], 0) # Extra vertex added to largest polygon, as expected assert len(polygons[0].segments) == 5 assert polygons[0].vertices[1] == Point2D(2, 2) assert polygons[0].segments[0].p2 == Point2D(2, 2) assert polygons[0].segments[1].p1 == Point2D(2, 2) assert len(polygon1.segments) == 4 # No extra vertex added assert len(polygon2.segments) == 4 # No extra vertex added