Skip to content

simplifyPolyline

Removes the points of a polyline that change its shape by less than tolerance, with the Ramer–Douglas–Peucker algorithm: each removed point is at most tolerance away from the simplified line. It shrinks map outlines or traced paths by orders of magnitude while keeping their look.

The first and last points are always kept. With a tolerance of 0, only points exactly on a straight line are removed. It is iterative, so very long polylines do not overflow the stack.

Available since v3.1.5

import { simplifyPolyline } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { simplifyPolyline } from 'helpers4/geometry';
simplifyPolyline(points: readonly Point[], tolerance: number): Point[]
ParameterTypeDescription
pointsreadonly Point[]The polyline
tolerancenumberThe maximum distance between a removed point and the result, in the units of the points

Point[] — The kept points, in their original order

A point closer than the tolerance to the simplified line is removed.

simplifyPolyline([{ x: 0, y: 0 }, { x: 1, y: 0.1 }, { x: 2, y: 0 }], 0.5)
// => [{ x: 0, y: 0 }, { x: 2, y: 0 }]

Pick the tolerance in the units of the points, e.g. a fraction of a pixel at the final scale.

const outline = simplifyPolyline(border, 0.5);

A point in 2D space.

interface Point {
  x: number;
  y: number;
}

View source on GitHub