Find a better strategy to avoid duplicate solutions in PathItem#getIntersections().

Closes #197.
This commit is contained in:
Jürg Lehni 2013-04-09 20:27:55 -07:00
parent 2c3e8a32ab
commit a9a0857ba4
3 changed files with 20 additions and 16 deletions

View file

@ -596,6 +596,14 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
&& rect.y < this.y + this.height; && rect.y < this.y + this.height;
}, },
touches: function(rect) {
rect = Rectangle.read(arguments);
return rect.x + rect.width >= this.x
&& rect.y + rect.height >= this.y
&& rect.x <= this.x + this.width
&& rect.y <= this.y + this.height;
},
/** /**
* {@grouptitle Boolean Operations} * {@grouptitle Boolean Operations}
* *

View file

@ -650,15 +650,7 @@ statics: {
strokeWidth: 0.1 strokeWidth: 0.1
}); });
/*#*/ } /*#*/ }
// We are not using Rectangle#intersects() here, since in order to if (bounds1.touches(bounds2)) {
// detect intersections that lie on curve bounds, we need to consider
// touching on one side of the tested rectangles as intersection as well
// If touch is condired at both sides, solutions lying on the border of
// bounds would turn up twice.
if (bounds1.x + bounds1.width >= bounds2.x
&& bounds1.y + bounds1.height >= bounds2.y
&& bounds1.x < bounds2.x + bounds2.width
&& bounds1.y < bounds2.y + bounds2.height) {
// See if both curves are flat enough to be treated as lines. // See if both curves are flat enough to be treated as lines.
if (Curve.isFlatEnough(v1, /*#=*/ Numerical.TOLERANCE) if (Curve.isFlatEnough(v1, /*#=*/ Numerical.TOLERANCE)
&& Curve.isFlatEnough(v2, /*#=*/ Numerical.TOLERANCE)) { && Curve.isFlatEnough(v2, /*#=*/ Numerical.TOLERANCE)) {
@ -673,12 +665,16 @@ statics: {
}); });
/*#*/ } /*#*/ }
// See if the parametric equations of the lines interesct. // See if the parametric equations of the lines interesct.
var point = new Line(v1[0], v1[1], v1[6], v1[7], false) var line1 = new Line(v1[0], v1[1], v1[6], v1[7], false),
.intersect(new Line(v2[0], v2[1], v2[6], v2[7], false)); line2 = new Line(v2[0], v2[1], v2[6], v2[7], false),
point = line1.intersect(line2);
// Filter out beginnings of the curves, to avoid duplicate
// solutions where curves join.
if (point && !point.equals(line1.point)
&& !point.equals(line2.point))
// Passing null for parameter leads to lazy determination of // Passing null for parameter leads to lazy determination of
// parameter values in CurveLocation#getParameter() only once // parameter values in CurveLocation#getParameter() only
// they are requested. // once they are requested.
if (point)
locations.push(new CurveLocation(curve, null, point)); locations.push(new CurveLocation(curve, null, point));
} else { } else {
// Subdivide both curves, and see if they intersect. // Subdivide both curves, and see if they intersect.

View file

@ -57,7 +57,7 @@ var PathItem = this.PathItem = Item.extend(/** @lends PathItem# */{
getIntersections: function(path) { getIntersections: function(path) {
// 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().intersects(path.getBounds())) if (!this.getBounds().touches(path.getBounds()))
return []; return [];
var locations = [], var locations = [],
curves1 = this.getCurves(), curves1 = this.getCurves(),