cutPolygonByGrid
Subdivides a polygon by intersecting with a uniform grid.
import {cutPolygonByGrid} from '@math.gl/polygon';
cutPolygonByGrid([0, 15, 15, 0, 0, -15, 0, 15], {size: 2, gridResolution: 20});
// returns [
// [15, 0, 0, -15, 0, 0, 15, 0],
// [15, 0, 0, 0, 0, 15, 15, 0]
// ]
]
Usage
cutPolygonByGrid(positions, holeIndices, [options]);
Arguments:
positions
(Array|TypedArray) - a flat array of the vertex positions that define the polygon's rings.holeIndices
(Array|TypedArray|null) - the indices inpositions
where each hole starts. Ifnull
, the polygon has no holes.options
(Object, optional)size
(Number) - the number of elements in each vertex. Size2
will interpretpositions
as[x0, y0, x1, y1, ...]
and size3
will interpretpositions
as[x0, y0, z0, x1, y1, z1, ...]
. Default2
.gridResolution
(Number, optional) - the grid size. Default10
.gridOffset
(Array, optional) - the grid offset in[x, y]
. Default[0, 0]
i.e. the grid starts from the coordinate origin.edgeTypes
(Boolean) - iftrue
, returns an additional array for each polygon that describes the nature of each vertex. See "returns" below.
Returns:
An array of polygons. Each polygons is represented by an object with the following fields:
-
positions
(Array|TypedArray) - a flat array of the vertex positions that define the polygon's rings. -
holeIndices
(Array|TypedArray) - the indices inpositions
where each hole starts. Not present if the polygon has no holes. -
edgeTypes
(Array) - describes the nature of each vertex inpositions
:0
- the segment connecting this vertex to the next is inside the original polygon1
- the segment connecting this vertex to the next is on the border of the original polygon