From 3a7c5286b51847a2c9d270036901185147fe6ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 28 Jul 2011 11:03:59 +0100 Subject: [PATCH] Improve precision of Line#intersect() and other parts that check for a divisor to not be 0, by comparing against the new Numerical.EPSILON rather than Numerical.TOLERANCE. --- src/basic/Line.js | 3 ++- src/path/PathFitter.js | 2 +- src/util/Numerical.js | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/basic/Line.js b/src/basic/Line.js index 59481a66..3f74d033 100644 --- a/src/basic/Line.js +++ b/src/basic/Line.js @@ -74,7 +74,8 @@ var Line = this.Line = Base.extend(/** @lends Line# */{ */ intersect: function(line) { var cross = this.vector.cross(line.vector); - if (Math.abs(cross) <= Numerical.TOLERANCE) + // Avoid divisions by 0, and errors when getting too close to 0 + if (Math.abs(cross) <= Numerical.EPSILON) return null; var v = line.point.subtract(this.point), t1 = v.cross(line.vector) / cross, diff --git a/src/path/PathFitter.js b/src/path/PathFitter.js index 6d012509..a9470220 100644 --- a/src/path/PathFitter.js +++ b/src/path/PathFitter.js @@ -93,7 +93,7 @@ var PathFitter = Base.extend({ // Use least-squares method to find Bezier control points for region. generateBezier: function(first, last, uPrime, tan1, tan2) { - var epsilon = Numerical.TOLERANCE, + var epsilon = Numerical.EPSILON, pt1 = this.points[first], pt2 = this.points[last], // Create the C and X matrices diff --git a/src/util/Numerical.js b/src/util/Numerical.js index 64e97861..043e4ac7 100644 --- a/src/util/Numerical.js +++ b/src/util/Numerical.js @@ -63,6 +63,9 @@ var Numerical = new function() { return { TOLERANCE: 10e-6, + // Precision when comparing against 0 + // TODO: Find a good value + EPSILON: 10e-12, /** * Gauss-Legendre Numerical Integration