Capsule.fromRect constructor

Capsule.fromRect({
  1. required Point center,
  2. required double width,
  3. required double height,
})

Create a capsule from center point, total width, total height.

The longer dimension becomes the medial axis direction. If width >= height, the capsule is horizontal; otherwise vertical.

Implementation

factory Capsule.fromRect({
  required Point center,
  required double width,
  required double height,
}) {
  if (width >= height) {
    final r = height / 2;
    final halfAxis = (width / 2) - r;
    return Capsule(
      medialAxis: Line(
        Point(center.x - halfAxis, center.y),
        Point(center.x + halfAxis, center.y),
      ),
      radius: r,
    );
  } else {
    final r = width / 2;
    final halfAxis = (height / 2) - r;
    return Capsule(
      medialAxis: Line(
        Point(center.x, center.y - halfAxis),
        Point(center.x, center.y + halfAxis),
      ),
      radius: r,
    );
  }
}