Further clean up Path.getBounds() code.

This commit is contained in:
Jürg Lehni 2012-12-27 17:43:21 +01:00
parent 3a1f2eec3f
commit 050ca1dcb1
2 changed files with 46 additions and 48 deletions

View file

@ -1874,32 +1874,29 @@ statics: {
if (!first) if (!first)
return new Rectangle(); return new Rectangle();
var coords = new Array(6), var coords = new Array(6),
prevCoords = new Array(6), // Make coordinates for first segment available in prevCoords.
roots = new Array(2); prevCoords = first._transformCoordinates(matrix, new Array(6), false),
// Make coordinates for first segment available in prevCoords. roots = new Array(2),
first._transformCoordinates(matrix, prevCoords, false); min = prevCoords.slice(0, 2),
var min = prevCoords.slice(0, 2),
max = min.slice(0), // clone max = min.slice(0), // clone
// Add some tolerance for good roots, as t = 0 / 1 are added // Add some tolerance for good roots, as t = 0 / 1 are added
// seperately anyhow, and we don't want joins to be added with // seperately anyhow, and we don't want joins to be added with
// radiuses in getStrokeBounds() // radiuses in getStrokeBounds()
tMin = Numerical.TOLERANCE, tMin = Numerical.TOLERANCE,
tMax = 1 - tMin; tMax = 1 - tMin;
function add(value, coord, padding) {
var left = value - padding,
right = value + padding;
if (left < min[coord])
min[coord] = left;
if (right > max[coord])
max[coord] = right;
}
function processSegment(segment) { function processSegment(segment) {
segment._transformCoordinates(matrix, coords, false); segment._transformCoordinates(matrix, coords, false);
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
function add(value, padding) {
var left = value - padding,
right = value + padding;
if (left < min[i])
min[i] = left;
if (right > max[i])
max[i] = right;
}
var v0 = prevCoords[i], // prev.point var v0 = prevCoords[i], // prev.point
v1 = prevCoords[i + 4], // prev.handleOut v1 = prevCoords[i + 4], // prev.handleOut
v2 = coords[i + 2], // segment.handleIn v2 = coords[i + 2], // segment.handleIn
@ -1912,12 +1909,10 @@ statics: {
c = v1 - v0; c = v1 - v0;
count = Numerical.solveQuadratic(a, b, c, roots, count = Numerical.solveQuadratic(a, b, c, roots,
Numerical.TOLERANCE); Numerical.TOLERANCE);
// Only add strokeWidth to bounds for points which lie within // Only add strokeWidth to bounds for points which lie within
// 0 < t < 1. The corner cases for cap and join are handled in // 0 < t < 1. The corner cases for cap and join are handled in
// getStrokeBounds() // getStrokeBounds()
add(v3, 0); add(v3, i, 0);
for (var j = 0; j < count; j++) { for (var j = 0; j < count; j++) {
var t = roots[j], var t = roots[j],
u = 1 - t; u = 1 - t;
@ -1928,6 +1923,7 @@ statics: {
+ 3 * u * u * t * v1 + 3 * u * u * t * v1
+ 3 * u * t * t * v2 + 3 * u * t * t * v2
+ t * t * t * v3, + t * t * t * v3,
i,
strokePadding ? strokePadding[i] : 0); strokePadding ? strokePadding[i] : 0);
} }
} }
@ -1936,6 +1932,7 @@ statics: {
prevCoords = coords; prevCoords = coords;
coords = tmp; coords = tmp;
} }
for (var i = 1, l = segments.length; i < l; i++) for (var i = 1, l = segments.length; i < l; i++)
processSegment(segments[i]); processSegment(segments[i]);
if (closed) if (closed)

View file

@ -393,35 +393,36 @@ var Segment = this.Segment = Base.extend(/** @lends Segment# */{
} }
// If no matrix was previded, this was just called to get the coords and // If no matrix was previded, this was just called to get the coords and
// we are done now. // we are done now.
if (!matrix) if (matrix) {
return; matrix._transformCoordinates(coords, 0, coords, 0, i / 2);
matrix._transformCoordinates(coords, 0, coords, 0, i / 2); x = coords[0];
x = coords[0]; y = coords[1];
y = coords[1]; if (change) {
if (change) { // If change is true, we need to set the new values back
// If change is true, we need to set the new values back point._x = x;
point._x = x; point._y = y;
point._y = y; i = 2;
i = 2; if (handleIn) {
if (handleIn) { handleIn._x = coords[i++] - x;
handleIn._x = coords[i++] - x; handleIn._y = coords[i++] - y;
handleIn._y = coords[i++] - y; }
} if (handleOut) {
if (handleOut) { handleOut._x = coords[i++] - x;
handleOut._x = coords[i++] - x; handleOut._y = coords[i++] - y;
handleOut._y = coords[i++] - y; }
} } else {
} else { // We want to receive the results in coords, so make sure
// We want to receive the results in coords, so make sure // handleIn and out are defined too, even if they're 0
// handleIn and out are defined too, even if they're 0 if (!handleIn) {
if (!handleIn) { coords[i++] = x;
coords[i++] = x; coords[i++] = y;
coords[i++] = y; }
} if (!handleOut) {
if (!handleOut) { coords[i++] = x;
coords[i++] = x; coords[i++] = y;
coords[i++] = y; }
} }
} }
return coords;
} }
}); });