Rename PathItem. _conditionIntersections() to _filterIntersections() and clean up code.

This commit is contained in:
Jürg Lehni 2014-02-20 15:38:31 +01:00
parent f1765d1cbf
commit e11b6138bd

View file

@ -35,8 +35,6 @@ var PathItem = Item.extend(/** @lends PathItem# */{
* @function * @function
* *
* @param {PathItem} path the other item to find the intersections with * @param {PathItem} path the other item to find the intersections with
* @param {Boolean} [sorted=true] controls wether to sort the results by
* offset
* @return {CurveLocation[]} the locations of all intersection between the * @return {CurveLocation[]} the locations of all intersection between the
* paths * paths
* @example {@paperscript} * @example {@paperscript}
@ -64,7 +62,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{
* } * }
* } * }
*/ */
getIntersections: function(path, expand) { getIntersections: function(path, _expand) {
// First check the bounds of the two paths. If they don't intersect, // First check the bounds of the two paths. If they don't intersect,
// we don't need to iterate through their curves. // we don't need to iterate through their curves.
if (!this.getBounds().touches(path.getBounds())) if (!this.getBounds().touches(path.getBounds()))
@ -87,10 +85,10 @@ var PathItem = Item.extend(/** @lends PathItem# */{
locations); locations);
} }
return PathItem._conditionIntersections(locations, expand); return PathItem._filterIntersections(locations, _expand);
}, },
getSelfIntersections: function(expand) { getSelfIntersections: function(_expand) {
var locations = [], var locations = [],
locs = [], locs = [],
curves = this.getCurves(), curves = this.getCurves(),
@ -149,7 +147,7 @@ var PathItem = Item.extend(/** @lends PathItem# */{
} }
} }
} }
return PathItem._conditionIntersections(locations, expand); return PathItem._filterIntersections(locations, _expand);
}, },
setPathData: function(data) { setPathData: function(data) {
@ -529,7 +527,7 @@ statics: {
return paths; return paths;
}, },
_conditionIntersections: function(locations, expand) { _filterIntersections: function(locations, _expand) {
function compare(loc1, loc2) { function compare(loc1, loc2) {
var path1 = loc1.getPath(), var path1 = loc1.getPath(),
path2 = loc2.getPath(); path2 = loc2.getPath();
@ -541,45 +539,38 @@ statics: {
// Sort by path id to group all locations on the same path. // Sort by path id to group all locations on the same path.
: path1._id - path2._id; : path1._id - path2._id;
} }
// Remove duplicate intersections near curve endings var max = locations.length - 1,
var loc, locNext, ONE = 1 - /*#=*/ Numerical.EPSILON,
tolerance = /*#=*/ Numerical.EPSILON,
tolerance1 = 1 - tolerance,
abs = Math.abs; abs = Math.abs;
// Merge intersections very close to the end of a curve to the // Merge intersections very close to the end of a curve to the begining
// begining of the next curve // of the next curve.
for (var i = locations.length-1; i >= 0; i--) { for (var i = max; i >= 0; i--) {
loc = locations[i]; var loc = locations[i],
locNext = loc._curve.getNext(); nextCurve = loc._curve.getNext(),
if (loc._parameter >= tolerance1 && locNext) { nextCurve2 = loc._curve2.getNext();
if (nextCurve && loc._parameter >= ONE) {
loc._parameter = 0; loc._parameter = 0;
loc._curve = locNext; loc._curve = nextCurve;
} }
locNext = loc._curve2.getNext(); if (nextCurve2 && loc._parameter2 >= ONE) {
if (loc._parameter2 >= tolerance1 && locNext) {
loc._parameter2 = 0; loc._parameter2 = 0;
loc._curve2 = locNext; loc._curve2 = nextCurve2;
} }
} }
if (locations.length > 1) { if (max > 0) {
locations.sort(compare); locations.sort(compare);
for (var length1 = locations.length - 1, i = length1; i >= 0; i--) { // Filter out duplicate locations
loc = locations[i]; for (var i = max; i >= 0; i--) {
locNext = (i === 0)? locations[length1] : locations[i-1]; if (locations[i].equals(i === 0
if (abs(loc._parameter - locNext._parameter) < tolerance && ? locations[max] : locations[i - 1])) {
loc._curve === locNext._curve &&
abs(loc._parameter2 - locNext._parameter2) < tolerance &&
loc._curve2 === locNext._curve2) {
locations.splice(i, 1); locations.splice(i, 1);
--length1; max--;
} }
} }
} }
if (expand) { if (_expand) {
for (var i = locations.length-1; i >= 0; i--) { for (var i = max; i >= 0; i--)
loc = locations[i]; locations.push(locations[i].getIntersection());
locations.push(loc.getIntersection());
}
locations.sort(compare); locations.sort(compare);
} }
return locations; return locations;