addRRect method

PathBuilder addRRect(
  1. Rect rect,
  2. double rx,
  3. double ry
)

Adds a rounded rectangle to the new path.

Implementation

PathBuilder addRRect(Rect rect, double rx, double ry) {
  if (rx == 0 && ry == 0) {
    return addRect(rect);
  }

  final Point magicRadius = Point(rx, ry) * _kArcApproximationMagic;

  moveTo(rect.left + rx, rect.top);

  // Top line.
  lineTo(rect.left + rect.width - rx, rect.top);

  // Top right arc.
  //
  cubicTo(
    rect.left + rect.width - rx + magicRadius.x,
    rect.top,
    rect.left + rect.width,
    rect.top + ry - magicRadius.y,
    rect.left + rect.width,
    rect.top + ry,
  );

  // Right line.
  lineTo(rect.left + rect.width, rect.top + rect.height - ry);

  // Bottom right arc.
  cubicTo(
    rect.left + rect.width,
    rect.top + rect.height - ry + magicRadius.y,
    rect.left + rect.width - rx + magicRadius.x,
    rect.top + rect.height,
    rect.left + rect.width - rx,
    rect.top + rect.height,
  );

  // Bottom line.
  lineTo(rect.left + rx, rect.top + rect.height);

  // Bottom left arc.
  cubicTo(
      rect.left + rx - magicRadius.x,
      rect.top + rect.height,
      rect.left,
      rect.top + rect.height - ry + magicRadius.y,
      rect.left,
      rect.top + rect.height - ry);

  // Left line.
  lineTo(rect.left, rect.top + ry);

  // Top left arc.
  cubicTo(
    rect.left,
    rect.top + ry - magicRadius.y,
    rect.left + rx - magicRadius.x,
    rect.top,
    rect.left + rx,
    rect.top,
  );

  close();
  return this;
}