From 15e00e0b99b5f59215028826a39321248f433d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 22 Jun 2019 23:05:50 +0200 Subject: [PATCH] Introduce Numerical.isMachineZero() Used in places requiring smaller epsilons for zero comparisons --- src/basic/Line.js | 4 ++-- src/path/Curve.js | 6 +++--- src/path/CurveLocation.js | 2 +- src/path/PathFitter.js | 2 +- src/path/PathItem.Boolean.js | 2 +- src/util/Numerical.js | 4 ++++ 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/basic/Line.js b/src/basic/Line.js index 98043e91..a77fa087 100644 --- a/src/basic/Line.js +++ b/src/basic/Line.js @@ -141,7 +141,7 @@ var Line = Base.extend(/** @lends Line# */{ } var cross = v1x * v2y - v1y * v2x; // Avoid divisions by 0, and errors when getting too close to 0 - if (!Numerical.isZero(cross)) { + if (!Numerical.isMachineZero(cross)) { var dx = p1x - p2x, dy = p1y - p2y, u1 = (v2x * dy - v2y * dx) / cross, @@ -175,7 +175,7 @@ var Line = Base.extend(/** @lends Line# */{ v2y = y - py, // ccw = v2.cross(v1); ccw = v2x * vy - v2y * vx; - if (!isInfinite && Numerical.isZero(ccw)) { + if (!isInfinite && Numerical.isMachineZero(ccw)) { // If the point is on the infinite line, check if it's on the // finite line too: Project v2 onto v1 and determine ccw based // on which side of the finite line the point lies. Calculate diff --git a/src/path/Curve.js b/src/path/Curve.js index 241df0e5..ac23371b 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -2091,7 +2091,7 @@ new function() { // Scope for bezier intersection using fat-line clipping return locations; } - function getLoopIntersection(v1, c1, locations, include) { + function getSelfIntersection(v1, c1, locations, include) { var info = Curve.classify(v1); if (info.type === 'loop') { var roots = info.roots; @@ -2131,7 +2131,7 @@ new function() { // Scope for bezier intersection using fat-line clipping } if (self) { // First check for self-intersections within the same curve. - getLoopIntersection(values1, curve1, locations, include); + getSelfIntersection(values1, curve1, locations, include); } // Check for intersections with other curves. // For self-intersection, we can start at i + 1 instead of 0. @@ -2314,7 +2314,7 @@ new function() { // Scope for bezier intersection using fat-line clipping var v1 = this.getValues(), v2 = curve && curve !== this && curve.getValues(); return v2 ? getCurveIntersections(v1, v2, this, curve, []) - : getLoopIntersection(v1, this, []); + : getSelfIntersection(v1, this, []); }, statics: /** @lends Curve */{ diff --git a/src/path/CurveLocation.js b/src/path/CurveLocation.js index 7ebd41c8..6adfda86 100644 --- a/src/path/CurveLocation.js +++ b/src/path/CurveLocation.js @@ -59,7 +59,7 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{ _setCurve: function(curve) { var path = curve._path; - // We only store the path to verify versions for cachd values. + // We only store the path to verify versions for cached values. // To ensure we use the right path (e.g. after splitting), we shall // always access the path on the result of getCurve(). this._path = path; diff --git a/src/path/PathFitter.js b/src/path/PathFitter.js index 27361e69..b5e6e14c 100644 --- a/src/path/PathFitter.js +++ b/src/path/PathFitter.js @@ -231,7 +231,7 @@ var PathFitter = Base.extend({ diff = pt.subtract(point), df = pt1.dot(pt1) + diff.dot(pt2); // u = u - f(u) / f'(u) - return Numerical.isZero(df) ? u : u - diff.dot(pt1) / df; + return Numerical.isMachineZero(df) ? u : u - diff.dot(pt1) / df; }, // Evaluate a bezier curve at a particular parameter value diff --git a/src/path/PathItem.Boolean.js b/src/path/PathItem.Boolean.js index 3534a224..f31a35b0 100644 --- a/src/path/PathItem.Boolean.js +++ b/src/path/PathItem.Boolean.js @@ -1172,7 +1172,7 @@ PathItem.inject(new function() { return inter && inter._overlap && inter._path === path; } - // First collect all overlaps and crossings while taking not of the + // First collect all overlaps and crossings while taking note of the // existence of both. var hasOverlaps = false, hasCrossings = false, diff --git a/src/util/Numerical.js b/src/util/Numerical.js index 54d4c5db..5510ef0d 100644 --- a/src/util/Numerical.js +++ b/src/util/Numerical.js @@ -168,6 +168,10 @@ var Numerical = new function() { return val >= -EPSILON && val <= EPSILON; }, + isMachineZero: function(val) { + return val >= -MACHINE_EPSILON && val <= MACHINE_EPSILON; + }, + /** * Returns a number whose value is clamped by the given range. *