class Geometry · Computations

Polygons

Computations on polygons: area, perimeter and orientation, containment, points and pins along the outline, and new polygons (regular, simplified, smoothed, parallel, slices).

Static methods

areastatic

static area(polygon: Polygon | ShapeLike, result?: Real | null): Real

Computes the absolute area of a polygon.

polygon
The polygon whose area is to be computed.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The absolute area enclosed by polygon.

besideCoordinatesstatic

static besideCoordinates(baseline: Polygon | ShapeLike, point: Point | VectorLike): [number, number]

Where a point lies beside the polygon: the ratio along it and the offset from it (positive on the left side, negative on the right), the inverse of pointBeside, e.g. to read a dragged handle as a position along a route.

pointBeside(baseline, ratio, offset, true) gives the point back. Where several positions reach the point (on the inner side of a bend), the one with the smallest offset wins. A point that no position reaches (behind a corner cut by the miter limit) gets the closest point of baseline as its ratio and the signed distance to it as its offset. An empty polygon or a NaN point gives NaN.

baseline
The polygon to measure along.
point
The point.

Returns The ratio along baseline (0 to 1) and the offset (positive = left).

boundingBoxstatic

static boundingBox(polygon: Polygon | ShapeLike, result?: Box | null): Box

Computes the axis-aligned bounding box of the polygon.

polygon
The polygon whose bounds are computed.
result optional
Optional Box to write the result into; it is returned. Without it, a new Box is returned.

Returns The smallest axis-aligned box that contains all vertices of polygon.

centroidstatic

static centroid(polygon: Polygon | ShapeLike, result?: Point | null): Point

Computes the geometric centroid of the polygon. For a non-self-intersecting polygon this is the center of mass of the enclosed area. Falls back to the vertex average for degenerate (zero-area) polygons.

polygon
The polygon whose centroid is computed.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The centroid of polygon.

closestPointstatic

static closestPoint(polygon: Polygon | ShapeLike, p: Point | VectorLike, result?: Point | null): Point

Computes the closest point on the polygon boundary to a given point.

polygon
The polygon to project onto.
p
The point to project.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The point on the boundary of polygon closest to p.

closestPositionstatic

static closestPosition(polygon: Polygon | ShapeLike, p: Point | VectorLike, result?: Real | null): Real

Computes the normalized position of the closest point on the polygon boundary to a given point.

polygon
The polygon to project onto.
p
The point to project.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns A value in [0, 1] representing the position along the boundary of polygon closest to p.

containsPointstatic

static containsPoint(polygon: Polygon | ShapeLike, p: Point | VectorLike, result?: Bool | null): Bool

Determines whether a given point lies inside the polygon. Points on the boundary (within 1e-9) count as inside, like Boxes.containsPoint; interior points are found with the ray-casting algorithm.

polygon
The polygon to test.
p
The point to test.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if p is inside or on the boundary of polygon; otherwise false.

copystatic

static copy(polygon: Polygon | ShapeLike, result?: Polygon | null): Polygon

Creates a copy of the specified polygon.

polygon
The polygon to copy.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns A copy of polygon.

directionAtstatic

static directionAt(polygon: Polygon | ShapeLike, ratio: Real | NumberLike, result?: Vector | null): Vector

Computes the tangent direction at a normalized position along the polygon boundary. At vertex joints the direction is averaged from the two adjacent edges.

polygon
The polygon to sample.
ratio
The position along the boundary: 0 and 1 are the first vertex; values outside [0, 1] wrap around (1.25 is the same position as 0.25).
result optional
Optional Vector to write the result into; it is returned. Without it, a new Vector is returned.

Returns The tangent direction at the specified ratio.

distanceTostatic

static distanceTo(polygon: Polygon | ShapeLike, p: Point | VectorLike, result?: Real | null): Real

Computes the shortest distance from a point to the polygon boundary.

polygon
The polygon to measure against.
p
The point to measure from.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The minimum distance from p to any edge of polygon.

edgeAtstatic

static edgeAt(polygon: Polygon | ShapeLike, index: number, result?: Segment | null): Segment

Returns the edge at the given index as a line segment. The index wraps around: index 0 is the edge from vertex 0 to vertex 1, and the last index connects the last vertex back to vertex 0. Negative indices wrap backwards.

polygon
The polygon to query.
index
The zero-based edge index (wraps modularly).
result optional
Optional Segment to write the result into; it is returned. Without it, a new Segment is returned.

Returns The line segment representing the edge at index.

edgeCoordinatesstatic

static edgeCoordinates(polygon: Polygon | ShapeLike, ratio: number): [number, number]

The edge of the polygon at a ratio and how far along it: [index, t], where edge index runs from vertex index to vertex index + 1 (the last edge closes the polygon back to vertex 0) and t goes from 0 to 1, e.g. to read data given per vertex at a ratio: const [i, t] = Polygons.edgeCoordinates(route, ratio); const v = data[i] + t * (data[(i + 1) % data.length] - data[i]).

The ratio is by length, as in pointAt (and pointBeside): pointAt(polygon, ratio) is the point at t on edge index. Ratios wrap around (1.25 is 0.25), as the polygon is closed. A ratio at an inner vertex k gives [k, 0]; edges without length are skipped. A polygon with one vertex gives [0, 0], an empty one or a NaN ratio NaN.

polygon
The polygon.
ratio
The ratio along it (by length).

Returns The edge index and the parameter on that edge (0 to 1).

isClockwisestatic

static isClockwise(polygon: Polygon | ShapeLike, result?: Bool | null): Bool

Determines whether the vertices of a polygon are oriented clockwise.

polygon
The polygon to evaluate.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if the polygon is oriented clockwise; otherwise false.

isClockwiseConvexstatic

static isClockwiseConvex(polygon: Polygon | ShapeLike, result?: Bool | null): Bool

Determines whether a polygon is convex and oriented clockwise.

polygon
The polygon to analyze.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if the polygon is convex and clockwise; otherwise false.

isConvexstatic

static isConvex(polygon: Polygon | ShapeLike, result?: Bool | null): Bool

Determines whether a polygon is convex, regardless of orientation.

polygon
The polygon to analyze.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if the polygon is convex; otherwise false.

isCounterClockwisestatic

static isCounterClockwise(polygon: Polygon | ShapeLike, result?: Bool | null): Bool

Determines whether the vertices of a polygon are oriented counterclockwise.

polygon
The polygon to evaluate.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if the polygon is oriented counterclockwise; otherwise false.

isCounterClockwiseConvexstatic

static isCounterClockwiseConvex(polygon: Polygon | ShapeLike, result?: Bool | null): Bool

Determines whether a polygon is convex and oriented counterclockwise.

polygon
The polygon to analyze.
result optional
Optional Bool to write the result into; it is returned. Without it, a new Bool is returned.

Returns true if the polygon is convex and counterclockwise; otherwise false.

normalAtstatic

static normalAt(polygon: Polygon | ShapeLike, ratio: Real | NumberLike, left: boolean, result?: Vector | null): Vector

Computes the normal direction at a normalized position along the polygon boundary. The normal is perpendicular to the tangent at that position.

polygon
The polygon to sample.
ratio
The position along the boundary: 0 and 1 are the first vertex; values outside [0, 1] wrap around (1.25 is the same position as 0.25).
left
If true, the normal points to the left of the travel direction; otherwise to the right.
result optional
Optional Vector to write the result into; it is returned. Without it, a new Vector is returned.

Returns The normal direction at the specified ratio.

orientedAreastatic

static orientedArea(polygon: Polygon | ShapeLike, result?: Real | null): Real

Computes the signed (oriented) area of a polygon.

polygon
The polygon whose oriented area is to be computed.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The oriented area: positive if counterclockwise, negative if clockwise.

parallelstatic

static parallel(baseline: Polygon | ShapeLike, offset: Real | NumberLike, left: Bool | BoolLike, result?: Polygon | null): Polygon

The parallel of the polygon: every vertex moved offset to the left or right side, e.g. height lines of a profile.

Same vertices and order as baseline, each moved along its corner's bisector so that the parallel stays offset away from every straight edge (miter, at most 4 × the offset: corners sharper than about 29° are cut). It runs through every pointBeside with the same offset. Sides as in pointBeside. On the inner side of a bend tighter than the offset the parallel folds over itself. Inside a concave polygon it can cross itself. With result, the values are written into its vertices (vertices are added or removed at the end), so a visual passed as result follows.

baseline
The polygon to follow.
offset
The distance from baseline.
left
true for the left side of the direction of travel, false for the right.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns The parallel, a polygon with the vertices of baseline moved.

perimeterstatic

static perimeter(polygon: Polygon | ShapeLike, result?: Real | null): Real

Computes the perimeter of a polygon (total length of all edges including the closing edge).

polygon
The polygon whose perimeter is to be computed.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The total length of all edges of polygon.

pinAtstatic

static pinAt(polygon: Polygon | ShapeLike, ratio: Real | NumberLike, result?: Pin | null): Pin

Computes the pin (point with orientation) at a normalized position along the polygon boundary. A ratio of 0 and 1 both correspond to the first vertex; the path travels along all edges including the closing edge. The pin's orientation is aligned with the tangent at that position.

polygon
The polygon to sample.
ratio
The position along the boundary: 0 and 1 are the first vertex; values outside [0, 1] wrap around (1.25 is the same position as 0.25).
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns The pin on the boundary of polygon at ratio.

pinAtDistancestatic

static pinAtDistance(polygon: Polygon | ShapeLike, distance: Real | NumberLike, wrapAround: boolean, result?: Pin | null): Pin

Computes the pin (point with orientation) at an absolute distance along the polygon boundary.

polygon
The polygon to sample.
distance
The distance along the boundary, measured in path-length units from the first vertex.
wrapAround
If true, distances beyond the perimeter wrap around, and negative ones wrap backwards from the end; otherwise clamped to the boundary.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns The pin on the boundary of polygon at the specified distance.

pinsAtstatic

static pinsAt(polygon: Polygon | ShapeLike, sortedRatios: Real[] | NumbersLike, result?: Pin[] | null): Pin[]

Computes pins at multiple normalized positions along the polygon boundary in a single walk.

polygon
The polygon to sample.
sortedRatios
A list of values in [0, 1], in any order (sorted input is fastest), specifying the positions along the boundary.
result optional
Optional array to fill: its entries are replaced by the result and its length is set. It is returned.

Returns A list of pins on the boundary of polygon, one per entry in sortedRatios, in the same order.

pinsAtDistancesstatic

static pinsAtDistances(polygon: Polygon | ShapeLike, sortedDistances: Real[] | NumbersLike, wrapAround: boolean, result?: Pin[] | null): Pin[]

Computes pins at multiple absolute distances along the polygon boundary in a single walk.

polygon
The polygon to sample.
sortedDistances
Distances along the boundary, measured in path-length units, in any order (sorted input is fastest).
wrapAround
If true, distances beyond the perimeter wrap around, and negative ones wrap backwards from the end; otherwise clamped to the boundary.
result optional
Optional array to fill: its entries are replaced by the result and its length is set. It is returned.

Returns A list of pins on the boundary of polygon, one per entry in sortedDistances, in the same order.

pointAtstatic

static pointAt(polygon: Polygon | ShapeLike, ratio: Real | NumberLike, result?: Point | null): Point

Computes the point at a normalized position along the polygon boundary. A ratio of 0 and 1 both correspond to the first vertex; the path travels along all edges including the closing edge.

polygon
The polygon to sample.
ratio
The position along the boundary: 0 and 1 are the first vertex; values outside [0, 1] wrap around (1.25 is the same position as 0.25).
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The point on the boundary of polygon at ratio.

pointBesidestatic

static pointBeside(baseline: Polygon | ShapeLike, ratio: Real | NumberLike, offset: Real | NumberLike, left: Bool | BoolLike, result?: Point | null): Point

The point beside the polygon at ratio along it, offset away on the left or right side, e.g. the top of an elevation profile drawn beside a route.

The ratio is by length, as in pointAt, including the closing edge; ratios wrap around (1.25 = 0.25). Sides are relative to the direction of travel: walking from the first vertex to the last on screen (y down), left is the left hand (a polygon running to the right has left = up). A negative offset goes to the other side. At a vertex the point lies on the corner's bisector, at the distance that keeps it offset away from both edges (miter, at most 4 × the offset: corners sharper than about 29° are cut); between two vertices it moves straight, so it is continuous in the ratio, lies on parallel with the same offset and stays exactly offset away from a straight edge. On a polygon that runs clockwise on screen (isClockwise), left is outside; counter-clockwise, left is inside. An empty polygon or a NaN ratio gives NaN.

baseline
The polygon to measure along.
ratio
The position along baseline, 0 = first vertex, 1 = first vertex again (wraps).
offset
The distance from baseline.
left
true for the left side of the direction of travel, false for the right.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The point beside baseline.

polygonstatic

static polygon(vertices: Sequence<Vector> | VectorsLike, result?: Polygon | null): Polygon

Constructs a polygon from a collection of vertices.

vertices
The points defining the polygon.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns A polygon composed of the provided vertices.

regularstatic

static regular(pin: Pin | PinLike, n: Integer | NumberLike, result?: Polygon | null): Polygon

Creates a regular polygon from a pin: centered at the pin's position, with its first vertex at the pin's tip, so the pin's length is the radius and its direction turns the polygon. E.g. a hexagon that follows a handle: Polygons.regular(Pins.fromSegment(center, handle), 6, hexagon).

The vertices follow clockwise on the screen. With 1 or 2 vertices the result is a point or a segment; fewer than 1 gives an empty polygon.

pin
The center (position), radius (length) and direction of the first vertex.
n
The number of vertices.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns The regular polygon.

reversestatic

static reverse(polygon: Polygon | ShapeLike, result?: Polygon | null): Polygon

Produces a polygon with vertices in reversed order, flipping the winding direction.

polygon
The polygon to reverse.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns A polygon with the opposite winding of polygon.

simplifystatic

static simplify(polygon: Polygon | ShapeLike, tolerance: number, result?: Polygon | null): Polygon

Simplifies the polygon by removing vertices that are within tolerance of the line between their neighbours (Ramer–Douglas–Peucker). The closed topology is preserved.

Tolerance 0 removes only vertices exactly on the line; a negative or a non-number tolerance keeps every vertex. For open lines and other strategies, see Simplify.

polygon
The polygon to simplify.
tolerance
The maximum perpendicular distance a vertex may have from the simplified line before it is kept.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns A polygon with fewer vertices that approximates polygon.

slicestatic

static slice(polygon: Polygon | ShapeLike, startRatio: Real | NumberLike, endRatio: Real | NumberLike, result?: Polyline | null): Polyline

Extracts a section of the polygon boundary as an open polyline between two normalized positions. If startRatio is greater than endRatio, the path wraps around past vertex 0.

polygon
The polygon to extract from.
startRatio
The start of the extraction range, in [0, 1].
endRatio
The end of the extraction range, in [0, 1].
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline representing the boundary section from startRatio to endRatio.

smoothstatic

static smooth(polygon: Polygon | ShapeLike, iterations: number, result?: Polygon | null): Polygon

Produces a smoothed polygon by iteratively applying Chaikin's corner-cutting algorithm. Each iteration replaces every edge with two new points at 25% and 75% of the edge. The vertex count doubles with each pass. The polygon remains closed.

polygon
The polygon to smooth.
iterations
The number of smoothing passes to apply.
result optional
Optional Polygon to write the result into; it is returned. Without it, a new Polygon is returned.

Returns A smoothed version of polygon.

toPolylinestatic

static toPolyline(polygon: Polygon | ShapeLike, result?: Polyline | null): Polyline

Converts the polygon to an open polyline with the same vertices. The closing edge is not included as an explicit segment.

polygon
The polygon to convert.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline with the same vertices as polygon.

vertexNormalstatic

static vertexNormal(polygon: Polygon | ShapeLike, index: number, left: boolean, result?: Vector | null): Vector

Computes the normal direction at a vertex by index. The normal is the vertex tangent bisector rotated 90 degrees.

polygon
The polygon to query.
index
The zero-based vertex index.
left
If true, the normal points to the left of the travel direction; otherwise to the right.
result optional
Optional Vector to write the result into; it is returned. Without it, a new Vector is returned.

Returns The normal direction at vertex index of polygon.

vertexOrientationstatic

static vertexOrientation(polygon: Polygon | ShapeLike, index: number, result?: Vector | null): Vector

Computes the tangent bisector direction at a vertex by index. At convex and concave corners this is the average of the two adjacent edge tangents. At near-180° turns it falls back to the outgoing edge direction.

polygon
The polygon to query.
index
The zero-based vertex index.
result optional
Optional Vector to write the result into; it is returned. Without it, a new Vector is returned.

Returns The tangent bisector direction at vertex index of polygon.