mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Switch to suggested new implementation of Line.getSignedDistance() by @iconexperience
Closes #554
This commit is contained in:
parent
8ae8855b81
commit
45c86a3035
2 changed files with 17 additions and 8 deletions
|
@ -165,12 +165,12 @@ var Line = Base.extend(/** @lends Line# */{
|
||||||
vx -= px;
|
vx -= px;
|
||||||
vy -= py;
|
vy -= py;
|
||||||
}
|
}
|
||||||
if (Numerical.isZero(vx))
|
return Numerical.isZero(vx)
|
||||||
return x - px;
|
? vy >= 0 ? py - x : x - px
|
||||||
var m = vy / vx, // slope
|
: Numerical.isZero(vy)
|
||||||
b = py - m * px; // y offset
|
? vx >= 0 ? y - py : py - y
|
||||||
// Distance to the linear equation
|
: -(vy * x - vx * y - px * (py + vy) + py * (px + vx)) /
|
||||||
return (y - (m * x) - b) / Math.sqrt(m * m + 1);
|
Math.sqrt(vx * vx + vy * vy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1062,7 +1062,6 @@ new function() { // Scope for methods that require numerical integration
|
||||||
var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7],
|
var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7],
|
||||||
tolerance = /*#=*/Numerical.TOLERANCE,
|
tolerance = /*#=*/Numerical.TOLERANCE,
|
||||||
hullEpsilon = 1e-9,
|
hullEpsilon = 1e-9,
|
||||||
getSignedDistance = Line.getSignedDistance,
|
|
||||||
// Calculate the fat-line L for Q is the baseline l and two
|
// Calculate the fat-line L for Q is the baseline l and two
|
||||||
// offsets which completely encloses the curve P.
|
// offsets which completely encloses the curve P.
|
||||||
d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]) || 0,
|
d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]) || 0,
|
||||||
|
@ -1176,6 +1175,17 @@ new function() { // Scope for methods that require numerical integration
|
||||||
}
|
}
|
||||||
|
|
||||||
/*#*/ if (__options.fatlineClipping) {
|
/*#*/ if (__options.fatlineClipping) {
|
||||||
|
function getSignedDistance(l1x, l1y, l2x, l2y, x, y) {
|
||||||
|
var vx = l2x - l1x,
|
||||||
|
vy = l2y - l1y;
|
||||||
|
if (Numerical.isZero(vx))
|
||||||
|
return vy >= 0 ? l1y - x : x - l1x;
|
||||||
|
var m = vy / vx, // slope
|
||||||
|
b = l1y - m * l1x; // y offset
|
||||||
|
// Distance to the linear equation
|
||||||
|
return (y - (m * x) - b) / Math.sqrt(m * m + 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the convex hull for the non-parametric bezier curve D(ti, di(t))
|
* Calculate the convex hull for the non-parametric bezier curve D(ti, di(t))
|
||||||
* The ti is equally spaced across [0..1] — [0, 1/3, 2/3, 1] for
|
* The ti is equally spaced across [0..1] — [0, 1/3, 2/3, 1] for
|
||||||
|
@ -1196,7 +1206,6 @@ new function() { // Scope for methods that require numerical integration
|
||||||
p2 = [ 2 / 3, dq2 ],
|
p2 = [ 2 / 3, dq2 ],
|
||||||
p3 = [ 1, dq3 ],
|
p3 = [ 1, dq3 ],
|
||||||
// Find signed distance of p1 and p2 from line [ p0, p3 ]
|
// Find signed distance of p1 and p2 from line [ p0, p3 ]
|
||||||
getSignedDistance = Line.getSignedDistance,
|
|
||||||
dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1),
|
dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1),
|
||||||
dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2),
|
dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2),
|
||||||
flip = false,
|
flip = false,
|
||||||
|
|
Loading…
Reference in a new issue