Skip to main content

@math.gl/geometry/polygon

From v3.2

@math.gl/geometry clips and splits polygons and polylines, computes winding and area, and adaptively subdivides geometry through coordinate transforms. Use it to prepare geometry before triangulation or rendering.

Usageโ€‹

import {earcut} from '@math.gl/geometry/polygon';

const positions = [0, 0, 1, 0, 1, 1, 0, 1];
const indices = earcut(positions, [], 2); // Two triangles

Featuresโ€‹

FeatureSupportNotes
Winding, signed area and ring traversalโœ…Polygon utilities define planar winding and area conventions.
Polygon triangulation with holesโœ…earcut consumes flat positions and explicit hole offsets.
Polyline/polygon clipping and grid cutsโœ…Clipping and grid/Mercator helpers split geometry at supported boundaries.
Planar path editing and polygon validityโœ…Planar line and polygon operations cover merging, densification, nearest-segment projection, and validation.
Projection-aware edge subdivisionโœ…subdividePolyline uses target-space tolerances and retains original-edge attribution.
Conforming triangle-mesh refinementโœ…subdivideTriangleMesh shares edge subdivisions and returns interpolation provenance.
Projected triangle-mesh clippingโœ…clipTriangleMesh supports disjoint convex target-plane regions, periodic copies and original-vertex weights; prepare geographic topology separately.
Certified continuous projection-error boundsโŒAdaptive sampling tests an approximation criterion; it does not prove a global continuous maximum error.
General Boolean topology and invalid-ring repairโŒClipping/triangulation do not supply a full union/difference/topology-repair engine for arbitrary malformed polygons.
Global geodesic and spherical polygon topologyโ—Use the separate geospatial seam/atlas APIs for their explicit edge models and supported domains.

Polygon playgroundโ€‹

Loading Polygon playgroundโ€ฆ

Open the interactive example.

Installationโ€‹

npm install @math.gl/geometry

Projection-aware subdivisionโ€‹

subdividePolyline subdivides source edges through a supplied coordinate transform using a tolerance in target units. It returns source and target coordinates plus edge attribution for interpolating other data. See the API reference for domains, limits, accuracy, and polygon-ring usage.

subdivideTriangleMesh refines indexed triangle meshes with shared-edge conformity and interpolation weights for UVs and other vertex attributes. It supports filled polygons after triangulation and textured bitmap meshes. See the mesh API reference.

See polygon utilities for winding and area conventions, and earcut for triangulation input and hole offsets.

Attributionโ€‹

The earcut implementation derives from Mapbox's earcut library under the ISC license. Source headers and distributed notices retain the applicable attribution.

clipTriangleMesh clips prepared target-plane triangles to disjoint convex regions, including periodic copies, and preserves original-vertex weights for UV and other attribute interpolation. Concave domains and holes can be supplied as triangulated regions. See the clipping API reference.

See Attributions for the project-wide source inventory and acknowledgments.

Polygon overlayโ€‹

unionPolygons, intersectPolygons, subtractPolygons, and xorPolygons compute planar Boolean operations. Each operand is a polygon (an exterior XY ring followed by holes) or a multipolygon. Each operation accepts one or more operands and returns a multipolygon. An empty result is []. Inputs use a shared Cartesian coordinate system.

import {intersectPolygons, subtractPolygons} from '@math.gl/geometry/polygon';

const visible = intersectPolygons(footprints, viewportPolygon);
const outside = subtractPolygons(footprints, viewportPolygon);