[Python] How do you triangulate a polygon in Shapely?

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    How do you triangulate a polygon in Shapely? Shapely actually offers a triangulate() function, but that only triangulates the vertices of the polygon as a point set. For convex polygons the solution is the same, but for non-convex you get extra triangles. I tried removing all triangles that are outside the polygon, but sometimes triangles can cross the polygon boundary (see below image). I guess I could remove those too, then substract the remaining triangles from the polygon and recursively triangulate the components of that difference. But is there a simpler way?

    Example where polygon triangulation is not a subset of vertices triangulation (polygon boundary wider and grey, triangle boundaries darker and thinner): [​IMG] Code to produce this geometry:

    from shapely.geometry import Polygon
    from shapely.ops import triangulate

    polygon = Polygon( [ (0,0), (0,3), (5,3), (2,4), (6,4), (6,0) ])
    delauney=triangulate(polygon)


    Update: The bug I filed got solved! I haven't tried it, so I won't close this question yet.

    Continue reading...

Compartilhe esta Página