JSFiddle - React, Tailwind, and code Playground
by iamjpg
HTML
<script src="https://rawgit.com/mourner/simplify-js/master/simplify.js"></script>
<script src="https://rawgit.com/adammiller/826148/raw/c8af0ad3a5e4cf01f3f08db851b06a1857fc4c63/douglasPeucker.js"></script>
<div id="map"></div>
<script src="//maps.google.com/maps/api/js?key=AIzaSyDciDh5LCPwyxG8tml6998d80mlukEj8Q4&libraries=drawing,places,geometry"></script>
CSS
#map {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
}
JavaScript
var poly_simplify = function(V, tol) {
// V ... [[x1,y1],[x2,y2],...] polyline
// tol ... approximation tolerance
// ==============================================
// Copyright 2002, softSurfer (www.softsurfer.com)
// This code may be freely used and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.
// http://softsurfer.com/Archive/algorithm_0205/algorithm_0205.htm
var sum = function(u,v) {return [u[0]+v[0], u[1]+v[1]];}
var diff = function(u,v) {return [u[0]-v[0], u[1]-v[1]];}
var prod = function(u,v) {return [u[0]*v[0], u[1]*v[1]];}
var dot = function(u,v) {return u[0]*v[0] + u[1]*v[1];}
var norm2 = function(v) {return v[0]*v[0] + v[1]*v[1];}
var norm = function(v) {return Math.sqrt(norm2(v));}
var d2 = function(u,v) {return norm2(diff(u,v));}
var d = function(u,v) {return norm(diff(u,v));}
var simplifyDP = function( tol, v, j, k, mk ) {
// This is the Douglas-Peucker recursive simplification routine
// It just marks vertices that are part of the simplified polyline
// for approximating the polyline subchain v[j] to v[k].
// mk[] ... array of markers matching vertex array v[]
if (k <= j+1) { // there is nothing to simplify
return;
}
// check for adequate approximation by segment S from v[j] to v[k]
var maxi = j; // index of vertex farthest from S
var maxd2 = 0; // distance squared of farthest vertex
var tol2 = tol * tol; // tolerance squared
S = [v[j], v[k]]; // segment from v[j] to v[k]
u = diff(S[1], S[0]); // segment direction vector
var cu = norm2(u,u); // segment length squared
// test each vertex v[i] for max distance from S
...