def test_subtract(): subject_polygon = CSG.from_polygons([[[10, 10], [100, 10], [50, 140]]]) clip_polygon = CSG.from_polygons([[[10, 100], [50, 10], [100, 100]]]) subject_polygon.subtract(clip_polygon) polygons = subject_polygon.subtract(clip_polygon).to_polygons() assert polygons is not None
def test_from_polygons(): poly_a = CSG.from_polygons([[[0, 0], [15, 0], [15, 15], [0, 15]]]) poly_b = CSG.from_polygons([[[30, 30], [10, 30], [10, 10], [30, 10]]]) polygons_a = poly_a.to_polygons() polygons_b = poly_b.to_polygons() assert len(polygons_a) == 1 assert len(polygons_b) == 1 assert len(polygons_a[0]) == 5 assert len(polygons_b[0]) == 5 assert polygons_a[0][0] == polygons_a[0][-1] assert polygons_b[0][0] == polygons_b[0][-1]
def test_manual_union(): poly_a = CSG.from_polygons([[ [0, 0], [15, 0], [15, 15], [0, 15] ]]) poly_b = CSG.from_polygons([[ [30, 30], [10, 30], [10, 10], [30, 10] ]]) a = Node(poly_a.clone().segments) assert len(a.all_segments()) == 4 b = Node(poly_b.clone().segments) assert len(a.all_segments()) == 4 a.invert() assert len(a.all_segments()) == 4 b.clip_to(a) assert len(b.all_segments()) == 5 b.invert() assert len(b.all_segments()) == 5 a.clip_to(b) a_all_segments = a.all_segments() assert len(a.all_segments()) == 5 b.clip_to(a) b_all_segments = b.all_segments() assert len(b.all_segments()) == 5 segs = b.all_segments() assert len(segs) == 5 a.build(segs) a_all_segments = a.all_segments() assert len(a.all_segments()) == 10 a.invert() a_all_segments = a.all_segments() assert len(a.all_segments()) == 10
def test_subtract_2(): poly_a = CSG.from_polygons([[ [0, 0], [15, 0], [15, 15], [0, 15] ]]) poly_b = CSG.from_polygons([[ [30, 30], [10, 30], [10, 10], [30, 10] ]]) polygons = poly_a.subtract(poly_b).to_polygons() assert polygons is not None assert len(polygons) == 1, len(polygons) assert len(polygons[0]) == 8, len(polygons[0])
def test_intersect(): poly_a = CSG.from_polygons([[ [0, 0], [15, 0], [15, 15], [0, 15] ]]) poly_b = CSG.from_polygons([[ [30, 30], [10, 30], [10, 10], [30, 10] ]]) polygons = poly_a.intersect(poly_b).to_polygons() assert polygons is not None assert len(polygons) == 1, len(polygons) assert len(polygons[0]) == 5, len(polygons[0])
def test_union(): poly_a = CSG.from_polygons([[ [0, 0], [15, 0], [15, 15], [0, 15] ]]) poly_b = CSG.from_polygons([[ [30, 30], [10, 30], [10, 10], [30, 10] ]]) union = poly_a.union(poly_b) polygons = union.to_polygons() assert polygons is not None assert len(polygons) == 1, polygons assert len(polygons[0]) == 11, len(polygons[0])
def csg_circle(position: Vector = Vector(0, 0), size: float = 100) -> CSG: polygons = [pol2cart(delta(i, stepcount), size) + position for i in range(stepcount)] return CSG.from_polygons([polygons])