calculateMaxBounds method
Like calculateBounds, this function calculates the axis-aligned bounds of the object and returns that rectangle. But this function determines the max dimension of the shape (by calculating the distance from its center to the start and midpoint of each curve) and returns a square which can be used to hold the object in any rotation. This function can be used, for example, to calculate the max size of a UI element meant to hold this shape in any rotation.
bounds is a buffer to hold the results. If not supplied, a temporary
buffer will be created.
Returns the axis-aligned max bounding box for this object, where the rectangles left, top, right, and bottom values will be stored in entries 0, 1, 2, and 3, in that order.
Implementation
List<double> calculateMaxBounds([List<double>? bounds]) {
bounds ??= List.filled(4, 0);
if (bounds.length < 4) {
throw ArgumentError('Required bounds size of 4.');
}
var maxDistSquared = 0.0;
for (var i = 0; i < cubics.length; i++) {
final cubic = cubics[i];
final anchorDistance =
distanceSquared(cubic.anchor0X - centerX, cubic.anchor0Y - centerY);
final middlePoint = cubic.pointOnCurve(0.5);
final middleDistance =
distanceSquared(middlePoint.x - centerX, middlePoint.y - centerY);
maxDistSquared =
math.max(maxDistSquared, math.max(anchorDistance, middleDistance));
}
final distance = math.sqrt(maxDistSquared);
bounds[0] = centerX - distance;
bounds[1] = centerY - distance;
bounds[2] = centerX + distance;
bounds[3] = centerY + distance;
return bounds;
}