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
// https://github.com/paperjs/paper.js/issues/799
return vx == 0
return vx === 0
? vy >= 0 ? px - x : x - px
: vy == 0
: vy === 0
? vx >= 0 ? y - py : py - y
: (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()
*/
isTouching: function() {
var t1 = this.getTangent(),
inter = this._intersection,
t2 = inter && inter.getTangent();
return t1 && t2 ? t1.isCollinear(t2) : false;
var inter = this._intersection;
if (inter && this.getTangent().isCollinear(inter.getTangent())) {
// Only consider two straight curves as touching if their lines
// 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(
_path1.getIntersections(_path2, function(inter) {
// Only handle overlaps when not self-intersecting
return inter.isCrossing() || _path2 && inter.isOverlap();
return _path2 && inter.isOverlap() || inter.isCrossing();
})
);
// console.timeEnd('intersection');
@ -161,9 +161,9 @@ PathItem.inject(new function() {
path1, path2, true);
}
function logIntersection(title, inter) {
function logIntersection(inter) {
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(),
'o', inter.isOverlap(), 'p', inter.getPoint(),
'Other', other._id, 'id', other.getPath()._id,
@ -216,7 +216,7 @@ PathItem.inject(new function() {
locations.forEach(function(inter) {
if (inter._other)
return;
logIntersection('Intersection', inter);
logIntersection(inter);
new Path.Circle({
center: inter.point,
radius: 2 * scaleFactor,
@ -292,17 +292,11 @@ PathItem.inject(new function() {
}
if (window.reportIntersections) {
console.log('After', locations.length / 2);
console.log('Split Crossings');
locations.forEach(function(inter) {
if (inter._other)
return;
logIntersection('Intersection', inter);
new Path.Circle({
center: inter.point,
radius: 2 * scaleFactor,
strokeColor: 'red',
strokeScaling: false
});
if (!inter._other) {
logIntersection(inter);
}
});
}
}