Skip to content

simplify

Simplifies the points of a polygon using both the Ramer-Douglas-Peucker algorithm and basic distance-based simplification. Adapted to an ES6 module from the excellent Simplify.js.

Removes points that do not change the shape by more than tolerance (in coordinate units) using the Ramer–Douglas–Peucker algorithm: the dashed line is the original, the solid one keeps only the points that matter at a tolerance of 4. Geomap simplifies polygons this way before drawing.

import {simplify} from "@d3plus/math";

const r = n => Math.round(n * 100) / 100;
const poly = Array.from({length: 21}, (_, i) => [
  20 + i * 11,
  r(100 + Math.sin(i / 2) * 40 + ((i % 3) - 1) * 3),
]);

simplify(poly, 4);

By default a quick radial-distance pass drops near-duplicate points before the main algorithm runs. highestQuality: true skips that pass, which can keep a few more points at the same tolerance and runs noticeably slower on large polygons.

import {simplify} from "@d3plus/math";

const r = n => Math.round(n * 100) / 100;
const poly = Array.from({length: 21}, (_, i) => [
  20 + i * 11,
  r(100 + Math.sin(i / 2) * 40 + ((i % 3) - 1) * 3),
]);

simplify(poly, 4, true);

A small tolerance keeps almost every point; a large one keeps only the turns, trading detail for far fewer vertices.

import {simplify} from "@d3plus/math";

const r = n => Math.round(n * 100) / 100;
const poly = Array.from({length: 21}, (_, i) => [
  20 + i * 11,
  r(100 + Math.sin(i / 2) * 40 + ((i % 3) - 1) * 3),
]);

simplify(poly, 1);
simplify(poly, 12);

Parameters

import {simplify} from "@d3plus/math";
polyrequiredArray.<Point>

An Array of points that represent a polygon.

tolerancenumberDefault 1

Affects the amount of simplification (in the same metric as the point coordinates).

highestQualitybooleanDefault false

Excludes distance-based preprocessing step which leads to highest quality simplification but runs ~10-20 times slower.