Skip to content
fill_polygon

fill_polygon

Mojo function 🡭

fill_polygon

fn def fill_polygon(mut canvas: Canvas, points: List[Point], color: Color, fill_rule: FillRule = FillRule.EVEN_ODD)

Fill a polygon’s interior with the scanline algorithm.

Y-extent per edge is half-open, [min(y0,y1), max(y0,y1)), the rule real rasterizers use (OpenGL/DirectX’s “top-left fill rule”), so adjacent shapes sharing an edge tile without a gap or a double-covered seam. Without it a vertex shared by two edges running in opposite y-directions counts twice, and a local extremum such as a triangle’s apex has to contribute zero net crossings.

One consequence: a polygon’s bottom-most row, when it is a horizontal edge (as in any axis-aligned rectangle), is not filled – both adjacent edges have that y as their excluded “max” endpoint. Matching fill_rect(x, y, width, height) exactly therefore needs asymmetric corners: (x, y), (x+width-1, y), (x+width-1, y+height), (x, y+height) – inclusive on the last column, one-past on the last row. The X-fill between a row’s crossing pair is fully inclusive; only the Y-extent is half-open.

Under either fill rule every pixel gets exactly one set_pixel call per row, including at a self-intersection.

Args:

  • canvas (Canvas): Canvas to fill into.
  • points (List[Point]): Polygon vertices, in order. Implicitly closed.
  • color (Color): Fill color.
  • fill_rule (FillRule): EVEN_ODD (default) or NONZERO – see FillRule.