mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
parent
fa0a474ec7
commit
74b22266f7
1 changed files with 37 additions and 32 deletions
|
@ -628,16 +628,17 @@ statics: /** @lends Curve */{
|
||||||
var p1 = v[coord],
|
var p1 = v[coord],
|
||||||
c1 = v[coord + 2],
|
c1 = v[coord + 2],
|
||||||
c2 = v[coord + 4],
|
c2 = v[coord + 4],
|
||||||
p2 = v[coord + 6];
|
p2 = v[coord + 6],
|
||||||
if (p1 < val && p2 < val && c1 < val && c2 < val
|
res = 0;
|
||||||
|| p1 > val && p2 > val && c1 > val && c2 > val) {
|
// If val is outside the curve values, no solution is possible.
|
||||||
// If val is outside the curve values, no solution is possible.
|
if ( !(p1 < val && p2 < val && c1 < val && c2 < val ||
|
||||||
return 0;
|
p1 > val && p2 > val && c1 > val && c2 > val)) {
|
||||||
|
var c = 3 * (c1 - p1),
|
||||||
|
b = 3 * (c2 - c1) - c,
|
||||||
|
a = p2 - p1 - c - b;
|
||||||
|
res = Numerical.solveCubic(a, b, c, p1 - val, roots, min, max);
|
||||||
}
|
}
|
||||||
var c = 3 * (c1 - p1),
|
return res;
|
||||||
b = 3 * (c2 - c1) - c,
|
|
||||||
a = p2 - p1 - c - b;
|
|
||||||
return Numerical.solveCubic(a, b, c, p1 - val, roots, min, max);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getTimeOf: function(v, point) {
|
getTimeOf: function(v, point) {
|
||||||
|
@ -789,41 +790,45 @@ statics: /** @lends Curve */{
|
||||||
* NOTE: padding is only used for Path.getBounds().
|
* NOTE: padding is only used for Path.getBounds().
|
||||||
*/
|
*/
|
||||||
_addBounds: function(v0, v1, v2, v3, coord, padding, min, max, roots) {
|
_addBounds: function(v0, v1, v2, v3, coord, padding, min, max, roots) {
|
||||||
|
// Code ported and further optimised from:
|
||||||
|
// http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
|
||||||
|
function add(value, padding) {
|
||||||
|
var left = value - padding,
|
||||||
|
right = value + padding;
|
||||||
|
if (left < min[coord])
|
||||||
|
min[coord] = left;
|
||||||
|
if (right > max[coord])
|
||||||
|
max[coord] = right;
|
||||||
|
}
|
||||||
|
|
||||||
padding /= 2; // strokePadding is in width, not radius
|
padding /= 2; // strokePadding is in width, not radius
|
||||||
var minPad = min[coord] - padding,
|
var minPad = min[coord] - padding,
|
||||||
maxPad = max[coord] + padding;
|
maxPad = max[coord] + padding;
|
||||||
// The curve can only extend the current bounds if at least one value
|
// Perform a rough bounds checking first: The curve can only extend the
|
||||||
// is outside the min-max range.
|
// current bounds if at least one value is outside the min-max range.
|
||||||
if (v0 < minPad || v1 < minPad || v2 < minPad || v3 < minPad
|
if ( v0 < minPad || v1 < minPad || v2 < minPad || v3 < minPad ||
|
||||||
|| v0 > maxPad || v1 > maxPad || v2 > maxPad || v3 > maxPad) {
|
v0 > maxPad || v1 > maxPad || v2 > maxPad || v3 > maxPad) {
|
||||||
// Code ported and further optimised from:
|
|
||||||
// http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
|
|
||||||
function add(value, padding) {
|
|
||||||
var left = value - padding,
|
|
||||||
right = value + padding;
|
|
||||||
if (left < min[coord])
|
|
||||||
min[coord] = left;
|
|
||||||
if (right > max[coord])
|
|
||||||
max[coord] = right;
|
|
||||||
}
|
|
||||||
if (v1 < v0 != v1 < v3 && v2 < v0 != v2 < v3) {
|
if (v1 < v0 != v1 < v3 && v2 < v0 != v2 < v3) {
|
||||||
// If values are sorted, the curve's extrema are v0 and v3
|
// If the values of a curve are sorted, the extrema are simply
|
||||||
|
// the start and end point.
|
||||||
add(v0, padding);
|
add(v0, padding);
|
||||||
add(v3, padding);
|
add(v3, padding);
|
||||||
} else {// Calculate derivative of our bezier polynomial, divided by 3.
|
} else {
|
||||||
// Doing so allows for simpler calculations of a, b, c and leads to the
|
// Calculate derivative of our bezier polynomial, divided by 3.
|
||||||
// same quadratic roots.
|
// Doing so allows for simpler calculations of a, b, c and leads
|
||||||
|
// to the same quadratic roots.
|
||||||
var a = 3 * (v1 - v2) - v0 + v3,
|
var a = 3 * (v1 - v2) - v0 + v3,
|
||||||
b = 2 * (v0 + v2) - 4 * v1,
|
b = 2 * (v0 + v2) - 4 * v1,
|
||||||
c = v1 - v0,
|
c = v1 - v0,
|
||||||
count = Numerical.solveQuadratic(a, b, c, roots),
|
count = Numerical.solveQuadratic(a, b, c, roots),
|
||||||
// Add some tolerance for good roots, as t = 0, 1 are added
|
// Add some tolerance for good roots, as t = 0, 1 are added
|
||||||
// separately anyhow, and we don't want joins to be added with radii
|
// separately anyhow, and we don't want joins to be added
|
||||||
// in getStrokeBounds()
|
// with radii in getStrokeBounds()
|
||||||
tMin = /*#=*/Numerical.CURVETIME_EPSILON,
|
tMin = /*#=*/Numerical.CURVETIME_EPSILON,
|
||||||
tMax = 1 - tMin;
|
tMax = 1 - tMin;
|
||||||
// Only add strokeWidth to bounds for points which lie within 0 < t < 1
|
// Only add strokeWidth to bounds for points which lie within 0
|
||||||
// The corner cases for cap and join are handled in getStrokeBounds()
|
// < t < 1 The corner cases for cap and join are handled in
|
||||||
|
// getStrokeBounds()
|
||||||
add(v3, 0);
|
add(v3, 0);
|
||||||
for (var i = 0; i < count; i++) {
|
for (var i = 0; i < count; i++) {
|
||||||
var t = roots[i],
|
var t = roots[i],
|
||||||
|
|
Loading…
Reference in a new issue