mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 10:48:38 -05:00
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:
parent
86a26db2a2
commit
26b3beed5c
2 changed files with 28 additions and 2 deletions
|
@ -119,5 +119,26 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
|
||||||
return this.infinite ? dist : Math.min(dist,
|
return this.infinite ? dist : Math.min(dist,
|
||||||
point.getDistance(this.point),
|
point.getDistance(this.point),
|
||||||
point.getDistance(this.point.add(this.vector)));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -753,8 +753,13 @@ 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 point = new Line(v1[0], v1[1], v1[6], v1[7], false)
|
||||||
.intersect(new Line(v2[0], v2[1], v2[6], v2[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) {
|
if (point) {
|
||||||
// Avoid duplicates when hitting segments (closed paths too)
|
// Avoid duplicates when hitting segments (closed paths too)
|
||||||
var first = locations[0],
|
var first = locations[0],
|
||||||
|
|
Loading…
Reference in a new issue