mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Merge pull request #895 from iconexperience/further-simplify-winding-calculation2
Further simplify and explain winding calculation in getWinding() (2)
This commit is contained in:
commit
18e00f6312
1 changed files with 32 additions and 25 deletions
|
@ -357,7 +357,7 @@ PathItem.inject(new function() {
|
||||||
// the last non-horizontal curve, which will be the previous
|
// the last non-horizontal curve, which will be the previous
|
||||||
// non-horizontal curve for the first curve of the loop.
|
// non-horizontal curve for the first curve of the loop.
|
||||||
prevWinding = last.winding,
|
prevWinding = last.winding,
|
||||||
prevEnd = last.values[6];
|
prevXEnd = last.values[6];
|
||||||
end = start + curve.length;
|
end = start + curve.length;
|
||||||
for (var i = start; i < end; i++) {
|
for (var i = start; i < end; i++) {
|
||||||
var curve = curves[i],
|
var curve = curves[i],
|
||||||
|
@ -369,37 +369,44 @@ PathItem.inject(new function() {
|
||||||
// compare the endpoints of the curve to determine if the
|
// compare the endpoints of the curve to determine if the
|
||||||
// ray from query point along +-x direction will intersect
|
// ray from query point along +-x direction will intersect
|
||||||
// the monotone curve.
|
// the monotone curve.
|
||||||
|
// horizontal curves with winding == 0 will be completely
|
||||||
|
// ignored
|
||||||
if (winding && (py >= yStart && py <= yEnd
|
if (winding && (py >= yStart && py <= yEnd
|
||||||
|| py >= yEnd && py <= yStart)) {
|
|| py >= yEnd && py <= yStart)) {
|
||||||
|
// calculate the x value for the ray's intersection
|
||||||
|
var x;
|
||||||
if (py === yStart) {
|
if (py === yStart) {
|
||||||
var x = values[0];
|
x = values[0];
|
||||||
if (winding * prevWinding < 0) {
|
} else if (py === yEnd) {
|
||||||
if (x > xBefore && x < xAfter) {
|
x = values[6];
|
||||||
++windLeft;
|
} else if (Curve.solveCubic(values, 1, py, roots, 0, 1) === 1) {
|
||||||
++windRight;
|
x = Curve.getPoint(values, roots[0]).x;
|
||||||
} else if (x < xBefore) {
|
|
||||||
windLeft += winding;
|
|
||||||
} else if (x > xAfter) {
|
|
||||||
windRight += winding;
|
|
||||||
}
|
}
|
||||||
} else if (x < xBefore && prevEnd > xBefore) {
|
if (x != null) {
|
||||||
windLeft += winding;
|
// count the intersection of the ray with the monotonic curve if
|
||||||
} else if (x > xAfter && prevEnd < xAfter) {
|
// - the crossing is not at the start of the curve
|
||||||
windRight += winding;
|
// - or the windings are opposite (intersect at a vertical extremum)
|
||||||
}
|
// - or the start of the current curve and the end of the prev
|
||||||
} else if (Curve.solveCubic(values, 1, py, roots, 0, 1)
|
// curve are on opposite sides of px
|
||||||
=== 1) {
|
var isWindingChange = winding === -prevWinding,
|
||||||
var x = Curve.getPoint(values, roots[0]).x;
|
countIntersection = py !== yStart || isWindingChange ||
|
||||||
|
(x - px) * (prevXEnd - px) < 0;
|
||||||
|
if (countIntersection) {
|
||||||
if (x < xBefore) {
|
if (x < xBefore) {
|
||||||
windLeft += winding;
|
windLeft += winding;
|
||||||
} else if (x > xAfter) {
|
} else if (x > xAfter) {
|
||||||
windRight += winding;
|
windRight += winding;
|
||||||
|
} else if (py === yStart && isWindingChange) {
|
||||||
|
// intersection is at a vertical extremum
|
||||||
|
++windLeft;
|
||||||
|
++windRight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Update previous winding and end coordinate whenever
|
// Update previous winding and end coordinate whenever
|
||||||
// we encountered a non-horizontal curve.
|
// the ray intersects a non-horizontal curve.
|
||||||
prevWinding = winding;
|
prevWinding = winding;
|
||||||
prevEnd = values[6];
|
prevXEnd = values[6];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue