Skip to Content
Bear documentation

polygon

Import from bear/geometry/polygon.

Interfaces

SliceCrossing

Defined in: bear/geometry/polygon.ts:138

One horizontal-ray crossing: where, and through which polygon edge.

Properties

PropertyTypeDescription
edgenumberIndex of the edge (pts[i] → pts[i+1]) the ray crossed.
xnumber-

Functions

centerPolygon()

function centerPolygon(points): object;

Defined in: bear/geometry/polygon.ts:58

Local footprint and insertion point, using the bounding-box center, not the centroid.

Parameters

ParameterType
pointsVec2[]

Returns

object

NameType
centerVec2
heightnumber
pointsVec2[]
widthnumber

ensureCCW()

function ensureCCW(pts): Vec2[];

Defined in: bear/geometry/polygon.ts:33

Return points in CCW order (reverse if currently CW).

Parameters

ParameterType
ptsVec2[]

Returns

Vec2[]


horizontalSliceCrossings()

function horizontalSliceCrossings(pts, y): SliceCrossing[];

Defined in: bear/geometry/polygon.ts:149

Cast a horizontal ray at the given Y through a polygon and return every edge crossing, sorted by X. Consecutive pairs bound the inside spans, and the edge index says which boundary each span ends on.

Parameters

ParameterType
ptsVec2[]
ynumber

Returns

SliceCrossing[]


horizontalSliceRanges()

function horizontalSliceRanges(pts, y): [number, number][];

Defined in: bear/geometry/polygon.ts:182

Cast a horizontal ray at the given Y through a polygon and return the X-ranges where the ray is inside the polygon.

Returns sorted, non-overlapping [xEnter, xExit] pairs.

Parameters

ParameterType
ptsVec2[]
ynumber

Returns

[number, number][]


insetPolygon()

function insetPolygon(pts, distance): Vec2[];

Defined in: bear/geometry/polygon.ts:81

Offset each edge inward by distance and recompute vertices.

For CCW winding, the inward normal of edge direction (dx, dy) is (-dy, dx). Each edge is shifted inward, then consecutive shifted edges are intersected to produce the new vertex positions.

Parameters

ParameterType
ptsVec2[]
distancenumber

Returns

Vec2[]


polygonBoundingBox()

function polygonBoundingBox(pts): object;

Defined in: bear/geometry/polygon.ts:38

Axis-aligned bounding box of a polygon.

Parameters

ParameterType
ptsVec2[]

Returns

object

NameType
maxXnumber
maxYnumber
minXnumber
minYnumber

polygonEdges()

function polygonEdges(pts): [Vec2, Vec2][];

Defined in: bear/geometry/polygon.ts:11

Return consecutive edge pairs, wrapping the last vertex back to the first.

Parameters

ParameterType
ptsVec2[]

Returns

[Vec2, Vec2][]


polygonSignedArea()

function polygonSignedArea(pts): number;

Defined in: bear/geometry/polygon.ts:21

Signed area via the shoelace formula. Positive = CCW, negative = CW.

Parameters

ParameterType
ptsVec2[]

Returns

number

On this page