class Geometry · Computations
Simplify
Reduces a line to fewer points, e.g. a long time series or an elevation profile drawn at screen
resolution. Every function returns the indices of the points to keep, in ascending order, so
other columns of the data (time, name, the original values for hovering) can be picked with the
same indices. Draw the result with Polylines.pick: Polylines.pick(points, Simplify.lttb(points, 500), line).
The functions compute once and are not reactive. For data that changes, call them in
Reactive.do with what the points are computed from as senders, and pass the same result every
time. Points whose x or y is not a finite number are never chosen as inner points. Data given as
two arrays can be turned into points with xs.map((x, i) => [x, ys[i]]). For a simplified
polyline or polygon without indices, see Polylines.simplify and Polygons.simplify
(Douglas–Peucker, closed paths included).
Static methods
douglasPeuckerstatic
static douglasPeucker(points: Sequence<Vector> | VectorsLike, tolerance: number, result?: number[] | null): number[]Keeps the points needed so that no point is farther than tolerance from the simplified line
(Douglas–Peucker), for any open path, e.g. a GPS track or a river.
The first and the last point are always kept. A point is kept if its distance to the line
through its kept neighbours is greater than the tolerance. Tolerance 0 removes only points
exactly on the line; a negative or a non-number tolerance keeps every point. Keeps the same
points as Polylines.simplify.
points- The points, e.g.
[[x, y], …]or a polyline. tolerance- The largest distance a removed point may have from the simplified line.
resultoptional- Optional array to store the indices in; its length is set to the number of indices.
Returns The indices of the kept points, ascending.
envelopestatic
static envelope(points: Sequence<Vector> | VectorsLike, width: number, options?: SimplifyEnvelopeOptions | null, result?: number[] | null): number[]Keeps the outline of a line seen from above (or below): per column of width the highest
point, joined so that no point lies above the simplified line, e.g. the skyline of an
elevation profile. Meant for lines with increasing x drawn on screen.
Columns are width wide in x, on a grid that starts at x = 0. From each column the point
farthest up on screen (smallest y; with side: "lower" farthest down) is kept, plus the
first and the last point; between them the line follows the convex hull of the points, so the
area under (above) the line covers every point and peaks never grow or shrink when the width
changes. The result has one point per column that holds points, the first and the last point,
and between two neighbouring column extremes the hull points that bulge beyond the straight
line between them, so more points than columns (on real elevation profiles about 1.3 to 3
times as many). Doubling widths are nested: the points kept for a width are also kept for
half that width, so zooming out only removes points and zooming in brings them back. For a
column of about 3 pixels at pxPerUnit pixels per x unit: width = 2 ** Math.ceil(Math.log2(3 / pxPerUnit)). The points must be sorted by x; for a line with gaps,
call it once per piece. A width that is not a positive number keeps every point; an infinite
width gives one column.
points- The points, e.g.
[[x, y], …]or a polyline, sorted by x. width- The width of a column in x.
optionsoptional- The side (see
SimplifyEnvelopeOptions). resultoptional- Optional array to store the indices in; its length is set to the number of indices.
Returns The indices of the kept points, ascending.
lttbstatic
static lttb(points: Sequence<Vector> | VectorsLike, n: number, result?: number[] | null): number[]Keeps n points that look like the whole line: Largest-Triangle-Three-Buckets. Peaks usually
survive. Meant for lines with increasing x (time series, profiles).
The first and the last point are always kept. The points in between are split into n − 2
buckets with the same number of points; from each bucket the point forming the largest
triangle with the point kept before it and the average of the next bucket is kept. n at
least the number of points gives every index, n = 1 the first, n = 2 the first and the
last, n ≤ 0 none. A bucket of only non-finite points adds nothing, so the result can be
shorter than n.
points- The points, e.g.
[[x, y], …]or a polyline. n- The number of points to keep.
resultoptional- Optional array to store the indices in; its length is set to the number of indices.
Returns The indices of the kept points, ascending.
minMaxstatic
static minMax(points: Sequence<Vector> | VectorsLike, n: number, result?: number[] | null): number[]Keeps the first, the last, the lowest and the highest point of every x range, at most n
points. With n = 4 × the drawn width in pixels the line looks the same as the full one,
extremes included. Meant for lines with increasing x.
The x range of the points is split into ⌊n / 4⌋ equal parts. n at least the number of
points gives every index; n < 4 works like lttb. Without any finite point the result is
empty.
points- The points, e.g.
[[x, y], …]or a polyline. n- The maximum number of points to keep.
resultoptional- Optional array to store the indices in; its length is set to the number of indices.
Returns The indices of the kept points, ascending.
