diff --git a/src/basic/Line.js b/src/basic/Line.js index 9d82b0c9..15224a70 100644 --- a/src/basic/Line.js +++ b/src/basic/Line.js @@ -75,7 +75,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{ intersect: function(line) { var cross = this.vector.cross(line.vector); // Avoid divisions by 0, and errors when getting too close to 0 - if (Math.abs(cross) <= Numerical.EPSILON) + if (Numerical.isZero(cross)) return null; var v = line.point.subtract(this.point), t1 = v.cross(line.vector) / cross, @@ -96,7 +96,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{ var v1 = this.vector, v2 = point.subtract(this.point), ccw = v2.cross(v1); - if (ccw == 0) { + if (ccw === 0) { ccw = v2.dot(v1); if (ccw > 0) { ccw = v2.subtract(v1).dot(v1); diff --git a/src/basic/Matrix.js b/src/basic/Matrix.js index 79d63868..73e07dab 100644 --- a/src/basic/Matrix.js +++ b/src/basic/Matrix.js @@ -446,7 +446,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{ */ _getDeterminant: function() { var det = this._a * this._d - this._b * this._c; - return isFinite(det) && Math.abs(det) > Numerical.EPSILON + return isFinite(det) && !Numerical.isZero(det) && isFinite(this._tx) && isFinite(this._ty) ? det : null; }, diff --git a/src/basic/Point.js b/src/basic/Point.js index 278ecc70..f38f1ab3 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -701,7 +701,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{ * @returns {Boolean} {@true both x and y are 0} */ isZero: function() { - return this.x == 0 && this.y == 0; + return Numerical.isZero(this.x) && Numerical.isZero(this.y); }, /** diff --git a/src/basic/Size.js b/src/basic/Size.js index 4450794f..bd6d23ce 100644 --- a/src/basic/Size.js +++ b/src/basic/Size.js @@ -360,7 +360,7 @@ var Size = this.Size = Base.extend(/** @lends Size# */{ * @return {Boolean} {@true both width and height are 0} */ isZero: function() { - return this.width == 0 && this.height == 0; + return Numerical.isZero(this.width) && Numerical.isZero(this.height); }, /** diff --git a/src/path/Curve.js b/src/path/Curve.js index b5321820..0aad6847 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -724,10 +724,7 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{ // with x-axis. if (isFlatEnough(w)) { var line = new Line(w[0], w[5], true); - // Compare the line's squared length with EPSILON. If we're - // below, #intersect() will return null because of division - // by near-zero. - return [ line.vector.getLength(true) <= Numerical.EPSILON + return [ Numerical.isZero(line.vector.getLength(true)) ? line.point.x : xAxis.intersect(line).x ]; } diff --git a/src/path/SegmentPoint.js b/src/path/SegmentPoint.js index 660a8a27..672268e5 100644 --- a/src/path/SegmentPoint.js +++ b/src/path/SegmentPoint.js @@ -51,7 +51,7 @@ var SegmentPoint = Point.extend({ // Provide our own version of Point#isZero() that does not use the x / y // accessors but the internal properties directly, for performance // reasons, since it is used a lot internally. - return this._x == 0 && this._y == 0; + return Numerical.isZero(this._x) && Numerical.isZero(this._y); }, setSelected: function(selected) { diff --git a/src/util/Numerical.js b/src/util/Numerical.js index bda03428..e27978a6 100644 --- a/src/util/Numerical.js +++ b/src/util/Numerical.js @@ -74,7 +74,15 @@ var Numerical = new function() { EPSILON: 10e-12, /** - * Gauss-Legendre Numerical Integration + * Check if the value is 0, within a tolerance defined by + * Numerical.EPSILON. + */ + isZero: function(val) { + return Math.abs(val) <= this.EPSILON; + }, + + /** + * Gauss-Legendre Numerical Integration. */ integrate: function(f, a, b, n) { var x = abscissas[n - 2],