Skip to content
FutureBlitz

    Install

    Import std.math.polygon

    std.math.polygon

    Polygons: a ring is a Vec2[] of corners, closed from the last back to the first, and a region is a Vec2[][] of rings, filled where they wind around a point other than 0 times, as SVG's nonzero rule fills it, so its outer rings go counterclockwise and its holes clockwise. Area, point-in-polygon, the convex hull, triangulation with holes, and union, intersection, difference, and offsetting, whose regions come out as counterclockwise outer rings and clockwise holes. The math is in Doubles.

    std/math/polygon.fb

    Functions

    PolygonArea function

    Public Function PolygonArea(ring: Vec2[]): Double

    The area of the ring, above 0 when it goes counterclockwise and below when it goes clockwise: the shoelace formula, in Doubles.

    std/math/polygon.fb, line 37

    PolygonArea function

    Public Function PolygonArea(rings: Vec2[][]): Double

    The area of the region, its outer rings' less its holes', as each ring's PolygonArea adds up.

    std/math/polygon.fb, line 48

    PointInPolygon function

    Public Function PointInPolygon(point: Vec2, ring: Vec2[]): Bool

    Whether the point is in the ring or on its edge, by the nonzero rule.

    std/math/polygon.fb, line 106

    PointInPolygon function

    Public Function PointInPolygon(point: Vec2, rings: Vec2[][]): Bool

    Whether the point is in the region or on one of its rings' edges, by the nonzero rule.

    std/math/polygon.fb, line 109

    ConvexHull function

    @PanicsAtCaller Public Function ConvexHull(points: Vec2[]): Vec2[]

    The smallest convex polygon holding every point, counterclockwise from the lowest of the leftmost, with no corner on a straight edge: Andrew's monotone chain. Fewer than three points that aren't on one line give those of them that are its ends. A corner that isn't finite panics.

    std/math/polygon.fb, line 123

    Triangulate function

    @PanicsAtCaller Public Function Triangulate(outer: Vec2[]): (Int, Int, Int)[]

    The triangles that fill the ring, each as indices of its corners, counterclockwise.

    std/math/polygon.fb, line 177

    Triangulate function

    @PanicsAtCaller Public Function Triangulate(outer: Vec2[], holes: Vec2[][]): (Int, Int, Int)[]

    The triangles that fill the outer ring less its holes, each as indices of its corners in the rings one after another, the outer ring's first, counterclockwise: Mapbox's earcut, without its z-order hash, which cuts off one convex corner with no other corner in its triangle at a time, once each hole is joined to the outer ring by a bridge from its leftmost corner to a corner it sees. A ring's last corner may repeat its first. An outer ring of fewer than three corners gives none, and a hole of fewer than three is left out. A corner that isn't finite panics.

    std/math/polygon.fb, line 190

    Union function

    @PanicsAtCaller Public Function Union(a: Vec2[][], b: Vec2[][]): Vec2[][]

    The region filled by either a or b, or both. A corner that isn't finite panics.

    std/math/polygon.fb, line 856

    Intersection function

    @PanicsAtCaller Public Function Intersection(a: Vec2[][], b: Vec2[][]): Vec2[][]

    The region filled by both a and b. A corner that isn't finite panics.

    std/math/polygon.fb, line 864

    Difference function

    @PanicsAtCaller Public Function Difference(a: Vec2[][], b: Vec2[][]): Vec2[][]

    The region filled by a but not by b. A corner that isn't finite panics.

    std/math/polygon.fb, line 872

    Offset function

    @PanicsAtCaller Public Function Offset(rings: Vec2[][], distance: Float, rounded: Bool = True): Vec2[][]

    The region grown by distance all around, or shrunk by a negative one: each ring's edges moved out along their normals, joined around its corners by arcs when rounded, else by miters up to twice the distance long, beyond which they're cut off, and what overlaps then merged, so a shrunk part too thin for the distance goes. A corner or a distance that isn't finite panics.

    std/math/polygon.fb, line 883

    Report an issue with this page or with FutureBlitz on GitHub.