generatePath method

Path generatePath({
  1. required Rect rect,
})

Generates the actual path for the diagonal shape.

Creates a rectangular path with one diagonal edge. The implementation handles all four possible positions (top, bottom, left, right) combined with both directions (left, right) to create the appropriate diagonal cut.

The diagonal is calculated using trigonometry to determine the perpendicular height needed for the angled cut based on the rectangle width and angle.

rect - The bounding rectangle that defines the drawing area.

Returns a Path object with the diagonal shape's geometric outline.

Implementation

Path generatePath({required Rect rect}) {
  final Path path = Path();

  final width = rect.width;
  final height = rect.height;

  final double diagonalAngleRadAbs = this.angle.angleRadians.abs();
  final bool isDirectionLeft = this.direction == DiagonalDirection.Left;
  final double perpendicularHeight = (rect.width * tan(diagonalAngleRadAbs));

  switch (this.position) {
    case DiagonalPosition.Bottom:
      if (isDirectionLeft) {
        path.moveTo(0, 0);
        path.lineTo(width, 0);
        path.lineTo(width, height - perpendicularHeight);
        path.lineTo(0, height);
        path.close();
      } else {
        path.moveTo(width, height);
        path.lineTo(0, height - perpendicularHeight);
        path.lineTo(0, 0);
        path.lineTo(width, 0);
        path.close();
      }
      break;
    case DiagonalPosition.Top:
      if (isDirectionLeft) {
        path.moveTo(width, height);
        path.lineTo(width, 0 + perpendicularHeight);
        path.lineTo(0, 0);
        path.lineTo(0, height);
        path.close();
      } else {
        path.moveTo(width, height);
        path.lineTo(width, 0);
        path.lineTo(0, 0 + perpendicularHeight);
        path.lineTo(0, height);
        path.close();
      }
      break;
    case DiagonalPosition.Right:
      if (isDirectionLeft) {
        path.moveTo(0, 0);
        path.lineTo(width, 0);
        path.lineTo(width - perpendicularHeight, height);
        path.lineTo(0, height);
        path.close();
      } else {
        path.moveTo(0, 0);
        path.lineTo(width - perpendicularHeight, 0);
        path.lineTo(width, height);
        path.lineTo(0, height);
        path.close();
      }
      break;
    case DiagonalPosition.Left:
      if (isDirectionLeft) {
        path.moveTo(0 + perpendicularHeight, 0);
        path.lineTo(width, 0);
        path.lineTo(width, height);
        path.lineTo(0, height);
        path.close();
      } else {
        path.moveTo(0, 0);
        path.lineTo(width, 0);
        path.lineTo(width, height);
        path.lineTo(0 + perpendicularHeight, height);
        path.close();
      }
      break;
  }
  return path;
}