mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
Clean up and simplify PathItem#_splitPath() code.
- Follow Paper.js variable scoping conventions (pretend we have Java-style variable scopes for better readability of code) - Break lines at 80 chars - Merge some duplicate code in segment handling.
This commit is contained in:
parent
b8fcc9b105
commit
72a27d9732
1 changed files with 61 additions and 50 deletions
|
@ -88,65 +88,76 @@ PathItem.inject({
|
||||||
|
|
||||||
_splitPath: function(_ixs, other) {
|
_splitPath: function(_ixs, other) {
|
||||||
// Sort function for sorting intersections in the descending order
|
// Sort function for sorting intersections in the descending order
|
||||||
function sortIx(a, b) { return b.parameter - a.parameter; }
|
function sortIx(a, b) {
|
||||||
other = other || false;
|
return b.parameter - a.parameter;
|
||||||
var i, j, k, l, len, ixs, ix, path, crv, vals;
|
|
||||||
var ixPoint, nuSeg;
|
|
||||||
var paths = {}, lastPathId = null;
|
|
||||||
for (i = 0, l = _ixs.length; i < l; i++) {
|
|
||||||
ix = (other)? _ixs[i].getIntersection() : _ixs[i];
|
|
||||||
if (!paths[ix.path.id]) {
|
|
||||||
paths[ix.path.id] = ix.path;
|
|
||||||
}
|
|
||||||
if (!ix.curve._ixParams) {ix.curve._ixParams = []; }
|
|
||||||
ix.curve._ixParams.push({ parameter: ix.parameter, pair: ix.getIntersection() });
|
|
||||||
}
|
}
|
||||||
for (k in paths) {
|
var paths = {};
|
||||||
if (!paths.hasOwnProperty(k)) { continue; }
|
for (var i = 0, l = _ixs.length; i < l; i++) {
|
||||||
path = paths[k];
|
var ix = other ? _ixs[i].getIntersection() : _ixs[i];
|
||||||
var lastNode = path.lastSegment, firstNode = path.firstSegment;
|
if (!paths[ix.path.id])
|
||||||
var nextNode = null, left = null, right = null, parts = null, isLinear;
|
paths[ix.path.id] = ix.path;
|
||||||
var handleIn, handleOut;
|
if (!ix.curve._ixParams)
|
||||||
|
ix.curve._ixParams = [];
|
||||||
|
ix.curve._ixParams.push({
|
||||||
|
parameter: ix.parameter,
|
||||||
|
pair: ix.getIntersection()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for (var k in paths) {
|
||||||
|
if (!paths.hasOwnProperty(k))
|
||||||
|
continue;
|
||||||
|
var path = paths[k],
|
||||||
|
lastNode = path.lastSegment,
|
||||||
|
firstNode = path.firstSegment,
|
||||||
|
nextNode = null;
|
||||||
while (nextNode !== firstNode) {
|
while (nextNode !== firstNode) {
|
||||||
nextNode = (nextNode)? nextNode.previous: lastNode;
|
nextNode = (nextNode)? nextNode.previous: lastNode;
|
||||||
if (nextNode.curve._ixParams) {
|
if (nextNode.curve._ixParams) {
|
||||||
ixs = nextNode.curve._ixParams;
|
var ixs = nextNode.curve._ixParams,
|
||||||
|
crv = nextNode.getCurve(),
|
||||||
|
isLinear = crv.isLinear(),
|
||||||
|
vals = null,
|
||||||
|
segment;
|
||||||
ixs.sort(sortIx);
|
ixs.sort(sortIx);
|
||||||
crv = nextNode.getCurve();
|
for (var i = 0, l = ixs.length; i < l; i++) {
|
||||||
isLinear = crv.isLinear();
|
var ix = ixs[i];
|
||||||
crv = vals = null;
|
if (!vals)
|
||||||
for (i = 0, l = ixs.length; i < l; i++) {
|
vals = crv.getValues();
|
||||||
ix = ixs[i];
|
if (ix.parameter === 0 || ix.parameter === 1) {
|
||||||
crv = nextNode.getCurve();
|
// Intersection is on an existing node: No need to
|
||||||
if (!vals) vals = crv.getValues();
|
// create a new segment, we just link the
|
||||||
if (ix.parameter === 0.0 || ix.parameter === 1.0) {
|
// corresponding intersections together
|
||||||
// Intersection is on an existing node
|
segment = ix.parameter === 0
|
||||||
// no need to create a new segment,
|
? crv.segment1
|
||||||
// we just link the corresponding intersections together
|
: crv.segment2;
|
||||||
nuSeg = (ix.parameter === 0.0)? crv.segment1 : crv.segment2;
|
|
||||||
nuSeg._ixPair = ix.pair;
|
|
||||||
nuSeg._ixPair._segment = nuSeg;
|
|
||||||
} else {
|
} else {
|
||||||
parts = Curve.subdivide(vals, ix.parameter);
|
var parts = Curve.subdivide(vals, ix.parameter),
|
||||||
left = parts[0];
|
left = parts[0],
|
||||||
right = parts[1];
|
right = parts[1],
|
||||||
handleIn = handleOut = null;
|
segment = new Segment(
|
||||||
ixPoint = new Point(right[0], right[1]);
|
new Point(right[0], right[1]));
|
||||||
if (!isLinear) {
|
if (!isLinear) {
|
||||||
crv.segment1.handleOut = new Point(left[2] - left[0], left[3] - left[1]);
|
crv.segment1.handleOut = new Point(
|
||||||
crv.segment2.handleIn = new Point(right[4] - right[6], right[5] - right[7]);
|
left[2] - left[0],
|
||||||
handleIn = new Point(left[4] - ixPoint.x, left[5] - ixPoint.y);
|
left[3] - left[1]);
|
||||||
handleOut = new Point(right[2] - ixPoint.x, right[3] - ixPoint.y);
|
crv.segment2.handleIn = new Point(
|
||||||
|
right[4] - right[6],
|
||||||
|
right[5] - right[7]);
|
||||||
|
segment.handleIn = new Point(
|
||||||
|
left[4] - left[6],
|
||||||
|
left[5] - left[7]);
|
||||||
|
segment.handleOut = new Point(
|
||||||
|
right[2] - left[6],
|
||||||
|
right[3] - left[7]);
|
||||||
}
|
}
|
||||||
nuSeg = new Segment(ixPoint, handleIn, handleOut);
|
path.insert(nextNode.index + 1, segment);
|
||||||
nuSeg._ixPair = ix.pair;
|
crv = nextNode.getCurve();
|
||||||
nuSeg._ixPair._segment = nuSeg;
|
vals = left;
|
||||||
path.insert(nextNode.index + 1, nuSeg);
|
|
||||||
}
|
}
|
||||||
for (j = i + 1; j < l; j++) {
|
segment._ixPair = ix.pair;
|
||||||
|
segment._ixPair._segment = segment;
|
||||||
|
for (var j = i + 1; j < l; j++)
|
||||||
ixs[j].parameter = ixs[j].parameter / ix.parameter;
|
ixs[j].parameter = ixs[j].parameter / ix.parameter;
|
||||||
}
|
|
||||||
vals = left;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,7 +249,7 @@ PathItem.inject({
|
||||||
_splitCache.intersections = ixs;
|
_splitCache.intersections = ixs;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._splitPath(ixs);
|
this._splitPath(ixs, false);
|
||||||
this._splitPath(ixs, true);
|
this._splitPath(ixs, true);
|
||||||
path1Id = _path1.id;
|
path1Id = _path1.id;
|
||||||
path2Id = _path2.id;
|
path2Id = _path2.id;
|
||||||
|
|
Loading…
Reference in a new issue