mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Improve CurveLocation#isTouching() to better handle straight lines.
This commit is contained in:
parent
1073340eeb
commit
1f476c2107
3 changed files with 20 additions and 20 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue