variablePath method

Path variablePath(
  1. Size size,
  2. double radius,
  3. int sides
)

Implementation

Path variablePath(Size size, double radius, int sides) {
  var path = Path();
  var angle = (math.pi * 2) / sides;

  Offset center = Offset(size.width / 2, size.height / 2);

  if (sides < 3) {
    // Draw a circle
    path.addOval(Rect.fromCircle(
      center: Offset(size.width / 2, size.height / 2),
      radius: radius,
    ));
  } else {
    // Draw a polygon
    Offset startPoint = Offset(radius * cos(-pi / 2), radius * sin(-pi / 2));

    path.moveTo(startPoint.dx + center.dx, startPoint.dy + center.dy);

    for (int i = 1; i <= sides; i++) {
      double x = radius * cos(angle * i - pi / 2) + center.dx;
      double y = radius * sin(angle * i - pi / 2) + center.dy;
      path.lineTo(x, y);
    }
    path.close();
  }
  return path;
}