Martin Bonde
Martin Bonde

Reputation: 556

Fill polygon with holes

I have a shapely Polygon with holes, that I want to fill using pycairo.

Is there an easy way to split this polygon into multiple polygons, without holes, that cover the same surface?

Or is there a better way to fill a polygon with holes using pycairo?

Upvotes: 2

Views: 1383

Answers (1)

Martin Bonde
Martin Bonde

Reputation: 556

I figured out a solution using the cairo clipping feature:

def fill_polygon(self, context, polygon):
    context.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)

    poly = polygon.exterior
    for x, y in poly.coords:
        context.line_to(x, y)
    context.clip_preserve()

    for poly in polygon.interiors:
        context.move_to(*poly.coords[-1])
        for x, y in poly.coords:
            context.line_to(x, y)

    context.fill()
    context.reset_clip()

Upvotes: 1

Related Questions