def test_polygons_to_graph(): # 012345 # 0 A--B-C # 1 | | | # 2 D--E-F # 3 | | # 4 | | # 5 G----H a = (0, 0) b = (3, 0) c = (5, 0) d = (0, 2) e = (3, 2) f = (5, 2) g = (0, 5) h = (5, 5) polygons = [ [a, b, e, d], [b, c, f, e], [d, f, h, g], ] graph = polygons_to_graph(polygons) assert_equal( graph, { a: {b, d}, b: {a, c, e}, c: {b, f}, d: {a, e, f, g}, e: {b, d, f}, f: {c, e, d, h}, g: {d, h}, h: {g, f}, } )
def test_disjoint(): # this test will be stuck in an infinite loop poly1 = [(8, -16), (8, 0), (0, 0), (0, -8)] poly2 = [(-12, 36), (0, 36), (0, 48), (-12, 48)] poly3 = [(-12, 12), (0, 12), (0, -8), (-12, 4)] polys = [poly1, poly2, poly3] graph = polygons_to_graph(polys) merge_corners_onto_borders(graph)
def draw_polygons(polygons): p = canoepaddle.Pen() p.fill_mode('#eee') for poly in polygons: p.move_to(poly[-1]) for point in poly: p.line_to(point) return p.paper if __name__ == '__main__': polygons = gen_polygons() graph = polygons_to_graph(polygons) merge_corners_onto_borders(graph) continue_straight_lines(graph) paper = canoepaddle.Paper() paper.merge(draw_polygons(polygons)) paper.merge(draw_graph(graph)) bounds = paper.bounds() bounds.left -= 2 bounds.right += 2 bounds.bottom -= 2 bounds.top += 2 paper.override_bounds(bounds) print(paper.format_svg(2))