class Geometry · Computations

Polylines

Computations on polylines: length, points and pins along them, slices and extensions, closest points, and new polylines (simplified, smoothed, parallel, clipped, joined).

Static methods

besideCoordinatesstatic

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

Where a point lies beside the polyline: 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 polyline or a NaN point gives NaN.

baseline
The polyline to measure along.
point
The point.

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

boundingBoxstatic

static boundingBox(polyline: Polyline | ShapeLike, result?: Box | null): Box

Computes the axis-aligned bounding box of the polyline.

polyline
The polyline 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 polyline.

clipToBoxstatic

static clipToBox(polyline: Polyline | ShapeLike, box: Box | ShapeLike, result?: [number, number][] | null): [number, number][]

The parts of the polyline inside a box, as ranges of ratios along it, e.g. the piece of a route visible in the map window (Polylines.slice(route, from, to) for each range).

Ratios are by length, as in pointAt and slice; the ranges are ascending and do not overlap. The border counts as inside; pieces that meet are one range, and touching the box in a single point gives no range. A polyline without length gives [[0, 1]] when its point is inside, otherwise nothing. Edges with a NaN coordinate count as outside. With result, the ranges are written into it (its length is set to their number).

polyline
The polyline to clip.
box
The box.
result optional
Optional array to store the ranges in.

Returns The ratio ranges inside box, each [from, to].

closestPointstatic

static closestPoint(polyline: Polyline | ShapeLike, p: Point | VectorLike, result?: Point | null): Point

Computes the closest point on the polyline to a given point.

polyline
The polyline 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 polyline closest to p.

closestPositionstatic

static closestPosition(polyline: Polyline | ShapeLike, p: Point | VectorLike, result?: Real | null): Real

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

polyline
The polyline 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 polyline closest to p.

concatenatestatic

static concatenate(a: Polyline | ShapeLike, b: Polyline | ShapeLike, result?: Polyline | null): Polyline

Concatenates two polylines end-to-end. If the last vertex of a coincides with the first vertex of b, the duplicate vertex is merged.

a
The first polyline.
b
The second polyline, appended after a.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline formed by joining a and b.

copystatic

static copy(polyline: Polyline | ShapeLike, result?: Polyline | null): Polyline

Creates a copy of the specified polyline.

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

Returns A copy of polyline.

directionAtstatic

static directionAt(polyline: Polyline | ShapeLike, ratio: Real | NumberLike, result?: Vector | null): Vector

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

polyline
The polyline to sample.
ratio
A value in [0, 1] specifying the position along the polyline.
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(polyline: Polyline | ShapeLike, p: Point | VectorLike, result?: Real | null): Real

Computes the shortest distance from a point to the polyline.

polyline
The polyline 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 point on polyline.

edgeCoordinatesstatic

static edgeCoordinates(polyline: Polyline | ShapeLike, ratio: number): [number, number]

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

The ratio is by length, as in pointAt (and clipToBox): pointAt(polyline, ratio) is the point at t on edge index. Ratios below 0 and above 1 are clamped to the ends, so index + 1 is always a vertex: the end gives the last edge with t = 1. A ratio at an inner vertex k gives [k, 0]; edges without length are skipped. A polyline with one vertex gives [0, 0], an empty one or a NaN ratio NaN.

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

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

endDirectionstatic

static endDirection(polyline: Polyline | ShapeLike, result?: Vector | null): Vector

Returns the direction of the last edge of the polyline.

polyline
The polyline to query.
result optional
Optional Vector to write the result into; it is returned. Without it, a new Vector is returned.

Returns A unit direction from the second-to-last vertex toward the last vertex of polyline.

endPointstatic

static endPoint(polyline: Polyline | ShapeLike, result?: Point | null): Point

Returns the last vertex of the polyline.

polyline
The polyline to query.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The last vertex of polyline.

extendstatic

static extend(polyline: Polyline | ShapeLike, startAmount: Real | NumberLike, endAmount: Real | NumberLike, result?: Polyline | null): Polyline

Extends the polyline from both ends along the tangent direction. Positive values lengthen the polyline; negative values shorten it. Interior vertices are not affected.

Shortening is clamped: an end moves at most to the other vertex of its edge, so that edge becomes a point. On a polyline with a single edge, shortening both ends by more than its length makes both vertices one point, where the two shortenings divide the edge.

polyline
The polyline to extend.
startAmount
The distance to extend (or shrink) from the start, along the first edge direction.
endAmount
The distance to extend (or shrink) from the end, along the last edge direction.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline with adjusted start and end points.

lenstatic

static len(polyline: Polyline | ShapeLike, result?: Real | null): Real

Computes the total geometric length of a polyline.

polyline
The polyline to measure.
result optional
Optional Real to write the result into; it is returned. Without it, a new Real is returned.

Returns The sum of the lengths of all segments in polyline.

mapOnPolylinestatic

static mapOnPolyline(polyline: Polyline | ShapeLike, target: Polyline | ShapeLike, result?: Polyline | null): Polyline

Maps one polyline onto another, producing a polyline whose geometry follows target while structurally corresponding to polyline.

polyline
The source polyline whose structure is preserved.
target
The polyline that provides the geometric path.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline whose vertices correspond to those of polyline, mapped along the shape of target.

normalAtstatic

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

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

polyline
The polyline to sample.
ratio
A value in [0, 1] specifying the position along the polyline.
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.

parallelstatic

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

The parallel of the polyline: 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. 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 polyline 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 Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

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

pickstatic

static pick(points: Sequence<Vector> | VectorsLike, indices: number[], result?: Polyline | null): Polyline

Makes a polyline from the points at the given indices, in the given order, e.g. to draw the result of Simplify: Polylines.pick(points, Simplify.lttb(points, 500), line).

Indices outside the points are skipped. The vertices are copies, so changing them doesn't change the points. With result, the values are written into its vertices (vertices are added or removed at the end), so a visual passed as result follows.

points
The points, e.g. [[x, y], …] or a polyline.
indices
The indices of the points to use.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline through the picked points.

pinAtstatic

static pinAt(polyline: Polyline | ShapeLike, ratio: Real | NumberLike, result?: Pin | null): Pin

Computes the pin (point with orientation) at a normalized position along the polyline. The pin's orientation is aligned with the tangent at that position.

polyline
The polyline to sample.
ratio
A value in [0, 1] where 0 corresponds to the first vertex and 1 to the last vertex.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns The pin on polyline at the specified ratio.

pinAtDistancestatic

static pinAtDistance(polyline: Polyline | ShapeLike, distance: Real | NumberLike, result?: Pin | null): Pin

Computes the pin (point with orientation) at an absolute distance along the polyline. Distances outside the polyline's length are clamped to the endpoints.

polyline
The polyline to sample.
distance
The distance along the polyline, measured in path-length units from the first vertex.
result optional
Optional Pin to write the result into; it is returned. Without it, a new Pin is returned.

Returns The pin on polyline at the specified distance.

pinsAtstatic

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

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

polyline
The polyline to sample.
sortedRatios
A list of values in [0, 1], in any order (sorted input is fastest), where 0 corresponds to the first vertex and 1 to the last vertex.
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 polyline, one per entry in sortedRatios, in the same order.

pinsAtDistancesstatic

static pinsAtDistances(polyline: Polyline | ShapeLike, sortedDistances: Real[] | NumbersLike, result?: Pin[] | null): Pin[]

Computes pins at multiple absolute distances along the polyline in a single walk. Distances outside the polyline's length are clamped to the endpoints.

polyline
The polyline to sample.
sortedDistances
Distances along the polyline, measured in path-length units, in any order (sorted input is fastest).
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 polyline, one per entry in sortedDistances, in the same order.

placeOnCircularArcstatic

static placeOnCircularArc(circle: Circle | ShapeLike, start: Vector | VectorLike, end: Vector | VectorLike, clockwise: Bool | BoolLike, count: number, result?: Polyline | null): Polyline

Constructs a polyline that lies along a circular arc.

circle
The circle defining the arc.
start
The direction from which the arc begins.
end
The direction at which the arc ends.
clockwise
If true, the arc is traced clockwise; otherwise counterclockwise.
count
The number of vertices used to approximate the arc.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline approximating the circular arc from start to end around circle.

pointAtstatic

static pointAt(polyline: Polyline | ShapeLike, ratio: Real | NumberLike, result?: Point | null): Point

Computes the point at a normalized position along the polyline.

polyline
The polyline to sample.
ratio
A value in [0, 1] where 0 corresponds to the first vertex and 1 to the last vertex.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The point on polyline at the specified ratio.

pointBesidestatic

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

The point beside the polyline 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; ratios below 0 and above 1 are clamped to the ends. 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 polyline 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. An empty polyline or a NaN ratio gives NaN.

baseline
The polyline to measure along.
ratio
The position along baseline, 0 = first vertex, 1 = last vertex.
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.

polylinestatic

static polyline(vertices: Sequence<Vector> | VectorsLike, result?: Polyline | null): Polyline

Constructs a polyline from a collection of vertices.

vertices
The sequence of points defining the polyline.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns A polyline composed of the provided vertices.

simplifystatic

static simplify(polyline: Polyline | ShapeLike, tolerance: number, result?: Polyline | null): Polyline

Simplifies the polyline by removing vertices that are within tolerance of the line between their neighbours (Ramer–Douglas–Peucker).

Tolerance 0 removes only vertices exactly on the line; a negative or a non-number tolerance keeps every vertex. For the indices of the kept vertices (to pick other data with them), use Simplify.douglasPeucker; for other strategies, see Simplify.

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

Returns A polyline with fewer vertices that approximates polyline.

slicestatic

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

Extracts the sub-polyline between two normalized positions.

polyline
The source polyline.
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 The portion of polyline between startRatio and endRatio.

slicePrefixstatic

static slicePrefix(polyline: Polyline | ShapeLike, position: Real | NumberLike, result?: Polyline | null): Polyline

Produces a polyline representing the portion from the start up to a normalized position.

polyline
The source polyline.
position
A value in [0, 1] where 0 returns an empty prefix and 1 returns the full polyline.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns The leading portion of polyline up to position.

sliceSuffixstatic

static sliceSuffix(polyline: Polyline | ShapeLike, position: Real | NumberLike, result?: Polyline | null): Polyline

Produces a polyline representing the portion from a normalized position to the end.

polyline
The source polyline.
position
A value in [0, 1] where 0 returns the full polyline and 1 returns just the last vertex.
result optional
Optional Polyline to write the result into; it is returned. Without it, a new Polyline is returned.

Returns The trailing portion of polyline from position onward.

smoothstatic

static smooth(polyline: Polyline | ShapeLike, iterations: number, result?: Polyline | null): Polyline

Produces a smoothed polyline 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 start and end points are preserved.

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

Returns A smoothed version of polyline.

startDirectionstatic

static startDirection(polyline: Polyline | ShapeLike, result?: Vector | null): Vector

Returns the direction of the first edge of the polyline.

polyline
The polyline to query.
result optional
Optional Vector to write the result into; it is returned. Without it, a new Vector is returned.

Returns A unit direction from the first vertex toward the second vertex of polyline.

startPointstatic

static startPoint(polyline: Polyline | ShapeLike, result?: Point | null): Point

Returns the first vertex of the polyline.

polyline
The polyline to query.
result optional
Optional Point to write the result into; it is returned. Without it, a new Point is returned.

Returns The first vertex of polyline.

toReversedPolylinestatic

static toReversedPolyline(polyline: Polyline | ShapeLike, result?: Polyline | null): Polyline

Produces a polyline with vertices in reversed order.

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

Returns A polyline whose vertices appear in reverse order from polyline.

vertexNormalstatic

static vertexNormal(polyline: Polyline | 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. At open endpoints the single adjacent edge direction is used instead of a bisector.

polyline
The polyline 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 polyline.

vertexOrientationstatic

static vertexOrientation(polyline: Polyline | 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 open endpoints only the single adjacent edge tangent is used. At near-180° turns it falls back to the outgoing edge direction.

polyline
The polyline 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 polyline.