Introduce Numerical.isMachineZero()

Used in places requiring smaller epsilons for zero comparisons
This commit is contained in:
Jürg Lehni 2019-06-22 23:05:50 +02:00
parent 978cd94a9e
commit 15e00e0b99
6 changed files with 12 additions and 8 deletions

View file

@ -141,7 +141,7 @@ var Line = Base.extend(/** @lends Line# */{
} }
var cross = v1x * v2y - v1y * v2x; var cross = v1x * v2y - v1y * v2x;
// 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 (!Numerical.isZero(cross)) { if (!Numerical.isMachineZero(cross)) {
var dx = p1x - p2x, var dx = p1x - p2x,
dy = p1y - p2y, dy = p1y - p2y,
u1 = (v2x * dy - v2y * dx) / cross, u1 = (v2x * dy - v2y * dx) / cross,
@ -175,7 +175,7 @@ var Line = Base.extend(/** @lends Line# */{
v2y = y - py, v2y = y - py,
// ccw = v2.cross(v1); // ccw = v2.cross(v1);
ccw = v2x * vy - v2y * vx; 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 // 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 // finite line too: Project v2 onto v1 and determine ccw based
// on which side of the finite line the point lies. Calculate // on which side of the finite line the point lies. Calculate

View file

@ -2091,7 +2091,7 @@ new function() { // Scope for bezier intersection using fat-line clipping
return locations; return locations;
} }
function getLoopIntersection(v1, c1, locations, include) { function getSelfIntersection(v1, c1, locations, include) {
var info = Curve.classify(v1); var info = Curve.classify(v1);
if (info.type === 'loop') { if (info.type === 'loop') {
var roots = info.roots; var roots = info.roots;
@ -2131,7 +2131,7 @@ new function() { // Scope for bezier intersection using fat-line clipping
} }
if (self) { if (self) {
// First check for self-intersections within the same curve. // 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. // Check for intersections with other curves.
// For self-intersection, we can start at i + 1 instead of 0. // 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(), var v1 = this.getValues(),
v2 = curve && curve !== this && curve.getValues(); v2 = curve && curve !== this && curve.getValues();
return v2 ? getCurveIntersections(v1, v2, this, curve, []) return v2 ? getCurveIntersections(v1, v2, this, curve, [])
: getLoopIntersection(v1, this, []); : getSelfIntersection(v1, this, []);
}, },
statics: /** @lends Curve */{ statics: /** @lends Curve */{

View file

@ -59,7 +59,7 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
_setCurve: function(curve) { _setCurve: function(curve) {
var path = curve._path; 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 // To ensure we use the right path (e.g. after splitting), we shall
// always access the path on the result of getCurve(). // always access the path on the result of getCurve().
this._path = path; this._path = path;

View file

@ -231,7 +231,7 @@ var PathFitter = Base.extend({
diff = pt.subtract(point), diff = pt.subtract(point),
df = pt1.dot(pt1) + diff.dot(pt2); df = pt1.dot(pt1) + diff.dot(pt2);
// u = u - f(u) / f'(u) // 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 // Evaluate a bezier curve at a particular parameter value

View file

@ -1172,7 +1172,7 @@ PathItem.inject(new function() {
return inter && inter._overlap && inter._path === path; 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. // existence of both.
var hasOverlaps = false, var hasOverlaps = false,
hasCrossings = false, hasCrossings = false,

View file

@ -168,6 +168,10 @@ var Numerical = new function() {
return val >= -EPSILON && val <= EPSILON; 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. * Returns a number whose value is clamped by the given range.
* *