mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Introduce Numerical.isZero(), for comparisons against 0 with a tolerance.
This commit is contained in:
parent
125fa1c051
commit
400b454177
7 changed files with 16 additions and 11 deletions
|
@ -75,7 +75,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
|
||||||
intersect: function(line) {
|
intersect: function(line) {
|
||||||
var cross = this.vector.cross(line.vector);
|
var cross = this.vector.cross(line.vector);
|
||||||
// Avoid divisions by 0, and errors when getting too close to 0
|
// Avoid divisions by 0, and errors when getting too close to 0
|
||||||
if (Math.abs(cross) <= Numerical.EPSILON)
|
if (Numerical.isZero(cross))
|
||||||
return null;
|
return null;
|
||||||
var v = line.point.subtract(this.point),
|
var v = line.point.subtract(this.point),
|
||||||
t1 = v.cross(line.vector) / cross,
|
t1 = v.cross(line.vector) / cross,
|
||||||
|
@ -96,7 +96,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
|
||||||
var v1 = this.vector,
|
var v1 = this.vector,
|
||||||
v2 = point.subtract(this.point),
|
v2 = point.subtract(this.point),
|
||||||
ccw = v2.cross(v1);
|
ccw = v2.cross(v1);
|
||||||
if (ccw == 0) {
|
if (ccw === 0) {
|
||||||
ccw = v2.dot(v1);
|
ccw = v2.dot(v1);
|
||||||
if (ccw > 0) {
|
if (ccw > 0) {
|
||||||
ccw = v2.subtract(v1).dot(v1);
|
ccw = v2.subtract(v1).dot(v1);
|
||||||
|
|
|
@ -446,7 +446,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
|
||||||
*/
|
*/
|
||||||
_getDeterminant: function() {
|
_getDeterminant: function() {
|
||||||
var det = this._a * this._d - this._b * this._c;
|
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)
|
&& isFinite(this._tx) && isFinite(this._ty)
|
||||||
? det : null;
|
? det : null;
|
||||||
},
|
},
|
||||||
|
|
|
@ -701,7 +701,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{
|
||||||
* @returns {Boolean} {@true both x and y are 0}
|
* @returns {Boolean} {@true both x and y are 0}
|
||||||
*/
|
*/
|
||||||
isZero: function() {
|
isZero: function() {
|
||||||
return this.x == 0 && this.y == 0;
|
return Numerical.isZero(this.x) && Numerical.isZero(this.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -360,7 +360,7 @@ var Size = this.Size = Base.extend(/** @lends Size# */{
|
||||||
* @return {Boolean} {@true both width and height are 0}
|
* @return {Boolean} {@true both width and height are 0}
|
||||||
*/
|
*/
|
||||||
isZero: function() {
|
isZero: function() {
|
||||||
return this.width == 0 && this.height == 0;
|
return Numerical.isZero(this.width) && Numerical.isZero(this.height);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -724,10 +724,7 @@ var Curve = this.Curve = Base.extend(/** @lends Curve# */{
|
||||||
// with x-axis.
|
// with x-axis.
|
||||||
if (isFlatEnough(w)) {
|
if (isFlatEnough(w)) {
|
||||||
var line = new Line(w[0], w[5], true);
|
var line = new Line(w[0], w[5], true);
|
||||||
// Compare the line's squared length with EPSILON. If we're
|
return [ Numerical.isZero(line.vector.getLength(true))
|
||||||
// below, #intersect() will return null because of division
|
|
||||||
// by near-zero.
|
|
||||||
return [ line.vector.getLength(true) <= Numerical.EPSILON
|
|
||||||
? line.point.x
|
? line.point.x
|
||||||
: xAxis.intersect(line).x ];
|
: xAxis.intersect(line).x ];
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,7 @@ var SegmentPoint = Point.extend({
|
||||||
// Provide our own version of Point#isZero() that does not use the x / y
|
// Provide our own version of Point#isZero() that does not use the x / y
|
||||||
// accessors but the internal properties directly, for performance
|
// accessors but the internal properties directly, for performance
|
||||||
// reasons, since it is used a lot internally.
|
// 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) {
|
setSelected: function(selected) {
|
||||||
|
|
|
@ -74,7 +74,15 @@ var Numerical = new function() {
|
||||||
EPSILON: 10e-12,
|
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) {
|
integrate: function(f, a, b, n) {
|
||||||
var x = abscissas[n - 2],
|
var x = abscissas[n - 2],
|
||||||
|
|
Loading…
Reference in a new issue