mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Expose Curve. getCurveLineIntersections() for curve offsetting.
This commit is contained in:
parent
8c3c771891
commit
fafbd9ad36
1 changed files with 24 additions and 19 deletions
|
@ -1960,36 +1960,38 @@ new function() { // Scope for intersection using bezier fat-line clipping
|
||||||
* line is on the X axis, and solve the implicit equations for the X axis
|
* line is on the X axis, and solve the implicit equations for the X axis
|
||||||
* and the curve.
|
* and the curve.
|
||||||
*/
|
*/
|
||||||
function addCurveLineIntersections(v1, v2, c1, c2, locations, param) {
|
function getCurveLineIntersections(v, px, py, vx, vy) {
|
||||||
var flip = Curve.isStraight(v1),
|
// Calculate angle to the x-axis (1, 0).
|
||||||
vc = flip ? v2 : v1,
|
var angle = Math.atan2(-vy, vx),
|
||||||
vl = flip ? v1 : v2,
|
|
||||||
lx1 = vl[0], ly1 = vl[1],
|
|
||||||
lx2 = vl[6], ly2 = vl[7],
|
|
||||||
// Rotate both curve and line around l1 so that line is on x axis.
|
|
||||||
ldx = lx2 - lx1,
|
|
||||||
ldy = ly2 - ly1,
|
|
||||||
// Calculate angle to the x-axis (1, 0).
|
|
||||||
angle = Math.atan2(-ldy, ldx),
|
|
||||||
sin = Math.sin(angle),
|
sin = Math.sin(angle),
|
||||||
cos = Math.cos(angle),
|
cos = Math.cos(angle),
|
||||||
// (rlx1, rly1) = (0, 0)
|
// (rlx1, rly1) = (0, 0)
|
||||||
// Calculate the curve values of the rotated curve.
|
// Calculate the curve values of the rotated curve.
|
||||||
rvc = [];
|
rv = [],
|
||||||
|
roots = [];
|
||||||
for(var i = 0; i < 8; i += 2) {
|
for(var i = 0; i < 8; i += 2) {
|
||||||
var x = vc[i] - lx1,
|
var x = v[i] - px,
|
||||||
y = vc[i + 1] - ly1;
|
y = v[i + 1] - py;
|
||||||
rvc.push(
|
rv.push(
|
||||||
x * cos - y * sin,
|
x * cos - y * sin,
|
||||||
x * sin + y * cos);
|
x * sin + y * cos);
|
||||||
}
|
}
|
||||||
// Solve it for y = 0. We need to include t = 0, 1 and let addLocation()
|
// Solve it for y = 0. We need to include t = 0, 1 and let addLocation()
|
||||||
// do the filtering, to catch important edge cases.
|
// do the filtering, to catch important edge cases.
|
||||||
var roots = [],
|
Curve.solveCubic(rv, 1, 0, roots, 0, 1);
|
||||||
count = Curve.solveCubic(rvc, 1, 0, roots, 0, 1);
|
return roots;
|
||||||
|
}
|
||||||
|
|
||||||
|
function addCurveLineIntersections(v1, v2, c1, c2, locations, param) {
|
||||||
|
var flip = Curve.isStraight(v1),
|
||||||
|
vc = flip ? v2 : v1,
|
||||||
|
vl = flip ? v1 : v2,
|
||||||
|
x1 = vl[0], y1 = vl[1],
|
||||||
|
x2 = vl[6], y2 = vl[7],
|
||||||
|
roots = getCurveLineIntersections(vc, x1, y1, x2 - x1, y2 - y1);
|
||||||
// NOTE: count could be -1 for infinite solutions, but that should only
|
// NOTE: count could be -1 for infinite solutions, but that should only
|
||||||
// happen with lines, in which case we should not be here.
|
// happen with lines, in which case we should not be here.
|
||||||
for (var i = 0; i < count; i++) {
|
for (var i = 0, l = roots.length; i < l; i++) {
|
||||||
// For each found solution on the rotated curve, get the point on
|
// For each found solution on the rotated curve, get the point on
|
||||||
// the real curve and with that the location on the line.
|
// the real curve and with that the location on the line.
|
||||||
var tc = roots[i],
|
var tc = roots[i],
|
||||||
|
@ -2201,6 +2203,9 @@ new function() { // Scope for intersection using bezier fat-line clipping
|
||||||
pairs = null;
|
pairs = null;
|
||||||
}
|
}
|
||||||
return pairs;
|
return pairs;
|
||||||
}
|
},
|
||||||
|
|
||||||
|
// Exposed for use in boolean offsetting
|
||||||
|
getCurveLineIntersections: getCurveLineIntersections
|
||||||
}};
|
}};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue