Rough bounds checking for _addBounds()

Performance of `_addBounds()` can be improved significantly by performing a rough bounds checking first. This is a cheap way to check if the curve can extend the current min or max values at all. Only if the check is passed, further Only if the current bounds can be extended by the curve's bounds, further calculation needs to be done.
Also, if the values of a curve are sorted, the extrema are simply the start and end point.
This commit is contained in:
Jan 2016-06-16 12:46:12 +02:00 committed by GitHub
parent eb752a43cd
commit c217e963b8

View file

@ -785,6 +785,12 @@ statics: /** @lends Curve */{
*/
_addBounds: function(v0, v1, v2, v3, coord, padding, min, max, roots) {
padding /= 2; // strokePadding is in width, not radius
var minPad = min[coord] - padding,
maxPad = max[coord] + padding;
// The curve can only extend the current bounds if at least one value
// is outside the min-max range.
if (v0 < minPad || v1 < minPad || v2 < minPad || v3 < minPad
|| 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) {
@ -795,7 +801,11 @@ statics: /** @lends Curve */{
if (right > max[coord])
max[coord] = right;
}
// Calculate derivative of our bezier polynomial, divided by 3.
if (v1 < v0 != v1 < v3 && v2 < v0 != v2 < v3) {
// If values are sorted, the curve's extrema are v0 and v3
add(v0, padding);
add(v3, padding);
} else {// Calculate derivative of our bezier polynomial, divided by 3.
// Doing so allows for simpler calculations of a, b, c and leads to the
// same quadratic roots.
var a = 3 * (v1 - v2) - v0 + v3,
@ -823,6 +833,8 @@ statics: /** @lends Curve */{
padding);
}
}
}
}
}}, Base.each(
['getBounds', 'getStrokeBounds', 'getHandleBounds'],
// NOTE: Although Curve.getBounds() exists, we are using Path.getBounds() to