1.1.1 • Published 12 months ago
@watercolorizer/visvalingam v1.1.1
@watercolorizer/visvalingam

Visvalingam–Whyatt algorithm for line simplification.
Getting Started
import { simplify } from '@watercolorizer/visvalingam';
type Vertex = { x: number; y: number };
const points: Vertex[] = [
/* ...your points here */
];
const areaOfTriangle = ([v0, v1, v2]: [Vertex, Vertex, Vertex]) =>
Math.abs((v0.x - v2.x) * (v1.y - v0.y) - (v0.x - v1.x) * (v2.y - v0.y));
// Simplify polyline in points to 10, removing the triangles that have the least area.
const sPoints = simplify(areaOfTriangle, points, 10);
This implementation is not opinionated about any particular vertex format, as long as associated the weightFn
is
compatible.
type LngLat = [lng: number, lat: number, elevation?: number];
const areaOf = (triangle: [LngLat, LngLat, LngLat]) =>
spherical.computeArea(triangle);
// Simplify out half the points of a polyline
const simplifiedPoints = simplify(
areaOf,
geodata.lineString,
geodata.lineString / 2,
);
More Info
- Line Simplification by Mike Bostock.
- Intersection Avoidance by Jason Davies.