aabbForTransformedRect function

Rect aabbForTransformedRect({
  1. required Rect localRect,
  2. required Offset position,
  3. required double rotationDeg,
  4. required double scaleX,
  5. required double scaleY,
})

Computes an axis-aligned bounding box for a transformed rectangle.

localRect is specified in the node's local space around the origin. Transform is applied in the order: scale -> rotate -> translate.

Implementation

Rect aabbForTransformedRect({
  required Rect localRect,
  required Offset position,
  required double rotationDeg,
  required double scaleX,
  required double scaleY,
}) {
  final corners = <Offset>[
    Offset(localRect.left, localRect.top),
    Offset(localRect.right, localRect.top),
    Offset(localRect.right, localRect.bottom),
    Offset(localRect.left, localRect.bottom),
  ];

  final scaled = corners
      .map((c) => Offset(c.dx * scaleX, c.dy * scaleY))
      .toList(growable: false);

  final rotated = nearZero(rotationDeg)
      ? scaled
      : scaled
            .map((c) => rotatePoint(c, Offset.zero, rotationDeg))
            .toList(growable: false);

  final translated = rotated.map((c) => c + position).toList(growable: false);

  return aabbFromPoints(translated);
}