localBounds method

Rect2D localBounds({
  1. bool includeStroke = true,
})

Cheap local AABB (optionally includes stroke expansion)

Implementation

Rect2D localBounds({bool includeStroke = true}) {
  if (cmds.isEmpty) return const Rect2D(0, 0, 0, 0);
  double minX = double.infinity, minY = double.infinity;
  double maxX = -double.infinity, maxY = -double.infinity;
  void acc(Vec2 v) {
    if (v.x < minX) minX = v.x;
    if (v.x > maxX) maxX = v.x;
    if (v.y < minY) minY = v.y;
    if (v.y > maxY) maxY = v.y;
  }

  for (final c in cmds) {
    switch (c.verb) {
      case PathVerb.moveTo:
      case PathVerb.lineTo:
        acc(c.p);
        break;
      case PathVerb.quadTo:
        acc(c.p);
        acc(c.c1!);
        break;
      case PathVerb.cubicTo:
        acc(c.p);
        acc(c.c1!);
        acc(c.c2!);
        break;
      case PathVerb.close:
        break;
    }
  }
  if (includeStroke && style.stroke != null && style.strokeWidth > 0) {
    final r = style.strokeWidth / 2.0;
    return Rect2D.fromLTRB(minX - r, minY - r, maxX + r, maxY + r);
  }
  return Rect2D.fromLTRB(minX, minY, maxX, maxY);
}