Implement static Line.intersect() and use it to speed up Curve.getIntersections()

Unfortunately doesn't seem to have any impact!
This commit is contained in:
Jürg Lehni 2013-05-04 10:22:10 -07:00
parent 4eff5bf45e
commit f704b00e00
2 changed files with 28 additions and 2 deletions

View file

@ -119,5 +119,26 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
return this.infinite ? dist : Math.min(dist,
point.getDistance(this.point),
point.getDistance(this.point.add(this.vector)));
},
statics: {
intersect: function(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, infinite) {
var adx = ax2 - ax1,
ady = ay2 - ay1,
bdx = bx2 - bx1,
bdy = by2 - by1,
dx = ax1 - bx1,
dy = ay1 - by1,
cross = bdy * adx - bdx * ady;
if (!Numerical.isZero(cross)) {
var ta = (bdx * dy - bdy * dx) / cross,
tb = (adx * dy - ady * dx) / cross;
if ((infinite || 0 <= ta && ta <= 1)
&& (infinite || 0 <= tb && tb <= 1))
return Point.create(
ax1 + ta * adx,
ay1 + ta * ady);
}
}
}
});

View file

@ -753,8 +753,13 @@ statics: {
});
/*#*/ }
// See if the parametric equations of the lines interesct.
var point = new Line(v1[0], v1[1], v1[6], v1[7], false)
.intersect(new Line(v2[0], v2[1], v2[6], v2[7], false));
// var point = new Line(v1[0], v1[1], v1[6], v1[7], false)
// .intersect(new Line(v2[0], v2[1], v2[6], v2[7], false));
// Use static version without creation of Line objects, but it
// doesn't seem to yield measurable speed improvements!
var point = Line.intersect(
v1[0], v1[1], v1[6], v1[7],
v2[0], v2[1], v2[6], v2[7], false);
if (point) {
// Avoid duplicates when hitting segments (closed paths too)
var first = locations[0],