diff --git a/docs/src/geometry/api/g/Polyline/prototype/simplify.html b/docs/src/geometry/api/g/Polyline/prototype/simplify.html index 3232a4958..30d2af322 100644 --- a/docs/src/geometry/api/g/Polyline/prototype/simplify.html +++ b/docs/src/geometry/api/g/Polyline/prototype/simplify.html @@ -3,4 +3,4 @@

This function modifies the original polyline and returns self. If the polyline has fewer than 3 points, the polyline is not modified.

-

By default, a point is considered non-essential only if it lies directly on the connection line between the previous and the following point. You can specify a tolerance range by providing a threshold value within the opt object. Polyline points that are closer to the connection line than this value (inclusive) are removed; points that are farther from the connection line (exclusive) are kept.

+

By default, a point is considered non-essential only if it lies directly on the connection line between the previous and the following point. You can specify a tolerance range by providing a threshold value within the opt object (the default is 1e-10). Polyline points that are closer to the connection line than this value (inclusive) are removed; points that are farther from the connection line (exclusive) are kept.

diff --git a/src/g/polyline.mjs b/src/g/polyline.mjs index 4dbdc17b3..430690935 100644 --- a/src/g/polyline.mjs +++ b/src/g/polyline.mjs @@ -351,7 +351,10 @@ Polyline.prototype = { if (points.length < 3) return this; // we need at least 3 points // TODO: we may also accept startIndex and endIndex to specify where to start and end simplification - const threshold = opt.threshold || 0; // = max distance of middle point from chord to be simplified + + // Due to the nature of the algorithm, we do not use 0 as the default value for `threshold` + // because of the rounding errors that can occur when comparing distances. + const threshold = opt.threshold || 1e-10; // = max distance of middle point from chord to be simplified // start at the beginning of the polyline and go forward let currentIndex = 0; diff --git a/test/geometry/polyline.js b/test/geometry/polyline.js index 9202827fe..ffc6b60cc 100644 --- a/test/geometry/polyline.js +++ b/test/geometry/polyline.js @@ -1041,6 +1041,8 @@ QUnit.module('polyline', function() { assert.equal((new g.Polyline(['10 0'])).simplify().serialize(), '10,0'); assert.equal((new g.Polyline('10,0')).simplify().serialize(), '10,0'); + assert.equal((new g.Polyline(['210 740', '250 740', '570 740'])).simplify().serialize(), '210,740 570,740'); + assert.equal((new g.Polyline()).simplify().serialize(), ''); });