From fb5f8c011501a7b55ba2dd0e4c6abd1e288eb23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 12 Sep 2015 10:35:47 +0200 Subject: [PATCH] Introduce GEOMETRIC_EPSILON, for isOrthogonal(), isCollinear() and overlap checks. Relates to #777 --- src/basic/Line.js | 4 +--- src/basic/Point.js | 10 ++-------- src/path/Curve.js | 2 +- src/path/Path.js | 3 +-- src/util/Numerical.js | 1 + 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/basic/Line.js b/src/basic/Line.js index 28c6883c..a31b2f28 100644 --- a/src/basic/Line.js +++ b/src/basic/Line.js @@ -113,10 +113,8 @@ var Line = Base.extend(/** @lends Line# */{ }, isCollinear: function(line) { - // TODO: Tests showed that 1e-10 might work well here, but we want to - // keep it in sync with Point#isCollinear() return this._vx * line._vy - this._vy * line._vx - < /*#=*/Numerical.TOLERANCE; + < /*#=*/Numerical.GEOMETRIC_EPSILON; }, statics: /** @lends Line */{ diff --git a/src/basic/Point.js b/src/basic/Point.js index d96e08e8..e2cd6b43 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -702,10 +702,7 @@ var Point = Base.extend(/** @lends Point# */{ * @return {Boolean} {@true it is collinear} */ isCollinear: function(point) { - // NOTE: Numerical.EPSILON is too small, breaking shape-path-shape - // conversion test. But tolerance is probably too large? - // TODO: Tests showed that 1e-10 might work well here. - return Math.abs(this.cross(point)) < /*#=*/Numerical.TOLERANCE; + return Math.abs(this.cross(point)) < /*#=*/Numerical.GEOMETRIC_EPSILON; }, // TODO: Remove version with typo after a while (deprecated June 2015) @@ -719,10 +716,7 @@ var Point = Base.extend(/** @lends Point# */{ * @return {Boolean} {@true it is orthogonal} */ isOrthogonal: function(point) { - // NOTE: Numerical.EPSILON is too small, breaking shape-path-shape - // conversion test. - // TODO: Test if 1e-10 works here too? See #isCollinear() - return Math.abs(this.dot(point)) < /*#=*/Numerical.TOLERANCE; + return Math.abs(this.dot(point)) < /*#=*/Numerical.GEOMETRIC_EPSILON; }, /** diff --git a/src/path/Curve.js b/src/path/Curve.js index b9bbea51..cd4c76e3 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -1614,7 +1614,7 @@ new function() { // Scope for intersection using bezier fat-line clipping var line1 = new Line(v1[0], v1[1], v1[6], v1[7]), line2 = new Line(v2[0], v2[1], v2[6], v2[7]); if (!line1.isCollinear(line2) || line1.getDistance(line2.getPoint()) - > /*#=*/Numerical.GEOMETRY_TOLERANCE) + > /*#=*/Numerical.GEOMETRIC_EPSILON) return false; } else if (straight1 ^ straight2) { // If one curve is straight, the other curve must be straight, too, diff --git a/src/path/Path.js b/src/path/Path.js index 0bc06d55..e56137a5 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -2502,7 +2502,6 @@ new function() { // PostScript-style drawing commands x = pt.x, y = pt.y, abs = Math.abs, - epsilon = /*#=*/Numerical.EPSILON, rx = abs(radius.width), ry = abs(radius.height), rxSq = rx * rx, @@ -2519,7 +2518,7 @@ new function() { // PostScript-style drawing commands } factor = (rxSq * rySq - rxSq * ySq - rySq * xSq) / (rxSq * ySq + rySq * xSq); - if (abs(factor) < epsilon) + if (abs(factor) < /*#=*/Numerical.EPSILON) factor = 0; if (factor < 0) throw new Error( diff --git a/src/util/Numerical.js b/src/util/Numerical.js index ed5cc1eb..8bfbc0a5 100644 --- a/src/util/Numerical.js +++ b/src/util/Numerical.js @@ -81,6 +81,7 @@ var Numerical = new function() { * range (see MACHINE_EPSILON). */ EPSILON: EPSILON, + GEOMETRIC_EPSILON: 1e-9, /** * MACHINE_EPSILON for a double precision (Javascript Number) is * 2.220446049250313e-16. (try this in the js console)