Improve CurveLocation#isTouching() to better handle straight lines.

This commit is contained in:
Jürg Lehni 2015-10-21 01:10:24 +02:00
parent 1073340eeb
commit 1f476c2107
3 changed files with 20 additions and 20 deletions

View file

@ -184,9 +184,9 @@ var Line = Base.extend(/** @lends Line# */{
} }
// Based on the error analysis by @iconexperience outlined in // Based on the error analysis by @iconexperience outlined in
// https://github.com/paperjs/paper.js/issues/799 // https://github.com/paperjs/paper.js/issues/799
return vx == 0 return vx === 0
? vy >= 0 ? px - x : x - px ? vy >= 0 ? px - x : x - px
: vy == 0 : vy === 0
? vx >= 0 ? y - py : py - y ? vx >= 0 ? y - py : py - y
: (vx * (y - py) - vy * (x - px)) / Math.sqrt(vx * vx + vy * vy); : (vx * (y - py) - vy * (x - px)) / Math.sqrt(vx * vx + vy * vy);
} }

View file

@ -365,10 +365,16 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @see #isCrossing() * @see #isCrossing()
*/ */
isTouching: function() { isTouching: function() {
var t1 = this.getTangent(), var inter = this._intersection;
inter = this._intersection, if (inter && this.getTangent().isCollinear(inter.getTangent())) {
t2 = inter && inter.getTangent(); // Only consider two straight curves as touching if their lines
return t1 && t2 ? t1.isCollinear(t2) : false; // don't intersect.
var curve1 = this.getCurve(),
curve2 = inter.getCurve();
return !(curve1.isStraight() && curve2.isStraight()
&& curve1.getLine().intersect(curve2.getLine()));
}
return false;
}, },
/** /**

View file

@ -119,7 +119,7 @@ PathItem.inject(new function() {
var intersections = CurveLocation.expand( var intersections = CurveLocation.expand(
_path1.getIntersections(_path2, function(inter) { _path1.getIntersections(_path2, function(inter) {
// Only handle overlaps when not self-intersecting // Only handle overlaps when not self-intersecting
return inter.isCrossing() || _path2 && inter.isOverlap(); return _path2 && inter.isOverlap() || inter.isCrossing();
}) })
); );
// console.timeEnd('intersection'); // console.timeEnd('intersection');
@ -161,9 +161,9 @@ PathItem.inject(new function() {
path1, path2, true); path1, path2, true);
} }
function logIntersection(title, inter) { function logIntersection(inter) {
var other = inter._intersection; var other = inter._intersection;
var log = [title, inter._id, 'id', inter.getPath()._id, var log = ['Intersection', inter._id, 'id', inter.getPath()._id,
'i', inter.getIndex(), 't', inter.getParameter(), 'i', inter.getIndex(), 't', inter.getParameter(),
'o', inter.isOverlap(), 'p', inter.getPoint(), 'o', inter.isOverlap(), 'p', inter.getPoint(),
'Other', other._id, 'id', other.getPath()._id, 'Other', other._id, 'id', other.getPath()._id,
@ -216,7 +216,7 @@ PathItem.inject(new function() {
locations.forEach(function(inter) { locations.forEach(function(inter) {
if (inter._other) if (inter._other)
return; return;
logIntersection('Intersection', inter); logIntersection(inter);
new Path.Circle({ new Path.Circle({
center: inter.point, center: inter.point,
radius: 2 * scaleFactor, radius: 2 * scaleFactor,
@ -292,17 +292,11 @@ PathItem.inject(new function() {
} }
if (window.reportIntersections) { if (window.reportIntersections) {
console.log('After', locations.length / 2); console.log('Split Crossings');
locations.forEach(function(inter) { locations.forEach(function(inter) {
if (inter._other) if (!inter._other) {
return; logIntersection(inter);
logIntersection('Intersection', inter); }
new Path.Circle({
center: inter.point,
radius: 2 * scaleFactor,
strokeColor: 'red',
strokeScaling: false
});
}); });
} }
} }