mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
New clip convex-hull routine
This commit is contained in:
parent
834c8d17a9
commit
6c5e18ae45
1 changed files with 50 additions and 2 deletions
|
@ -1380,6 +1380,8 @@ new function() { // Scope for methods that require numerical integration
|
|||
* Calculating convex-hull is much easier than a set of arbitrary points.
|
||||
*
|
||||
* The convex-hull is returned as two parts [TOP, BOTTOM]:
|
||||
* (TOP and BOTTOM are in a coordinate space where y increases
|
||||
* upwards with origin at bottom-left)
|
||||
* part that lies above the 'median' (line connecting end points of the curve)
|
||||
* and part that lies below the median.
|
||||
*/
|
||||
|
@ -1434,7 +1436,51 @@ new function() { // Scope for methods that require numerical integration
|
|||
/**
|
||||
* Clips the convex-hull and returns [tmin, tmax] for the curve contained
|
||||
*/
|
||||
function clipConvexHull (hull, dmin, dmax) {
|
||||
function clipConvexHull(hull, dmin, dmax) {
|
||||
function clipCHull(hull_top, hull_bottom, dmin, dmax) {
|
||||
var tProxy, tVal = null, i, li, px, py, qx, qy;
|
||||
for (i = 0, li = hull_bottom.length-1; i < li; i++) {
|
||||
py = hull_bottom[i][1];
|
||||
qy = hull_bottom[i+1][1];
|
||||
if (py < qy)
|
||||
tProxy = null;
|
||||
else if (qy <= dmax) {
|
||||
px = hull_bottom[i][0];
|
||||
qx = hull_bottom[i+1][0];
|
||||
tProxy = px + (dmax - py) * (qx - px) / (qy - py);
|
||||
} else
|
||||
// Try the next chain
|
||||
continue;
|
||||
// We got a proxy-t;
|
||||
break;
|
||||
}
|
||||
if (hull_top[0][1] <= dmax)
|
||||
tProxy = hull_top[0][0];
|
||||
for (i = 0, li = hull_top.length-1; i < li; i++) {
|
||||
py = hull_top[i][1];
|
||||
qy = hull_top[i+1][1];
|
||||
if (py >= dmin)
|
||||
tVal = tProxy;
|
||||
else if (py > qy)
|
||||
tVal = null;
|
||||
else if (qy >= dmin) {
|
||||
px = hull_top[i][0];
|
||||
qx = hull_top[i+1][0];
|
||||
tVal = px + (dmin - py) * (qx - px) / (qy - py);
|
||||
} else
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
return tVal;
|
||||
}
|
||||
|
||||
var tmin, tmax, top = hull[0], bottom = hull[1];
|
||||
tmin = clipCHull(top, bottom, dmin, dmax);
|
||||
top.reverse();
|
||||
bottom.reverse();
|
||||
tmax = clipCHull(top, bottom, dmin, dmax);
|
||||
|
||||
return [tmin, tmax];
|
||||
}
|
||||
/*#*/ } // __options.fatline
|
||||
|
||||
|
@ -1536,6 +1582,8 @@ new function() { // Scope for methods that require numerical integration
|
|||
return locations;
|
||||
},
|
||||
|
||||
getConvexHull: getConvexHull
|
||||
getConvexHull: getConvexHull,
|
||||
|
||||
clipConvexHull: clipConvexHull
|
||||
}};
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue