RoundedPolygon.circle constructor

RoundedPolygon.circle({
  1. int numVertices = 8,
  2. double radius = 1.0,
  3. Offset center = .zero,
})

Creates a circle of the given radius about center, approximated by rounding a polygon of numVertices vertices.

Throws ArgumentError if numVertices is less than 3.

Implementation

factory circle({
  int numVertices = 8,
  double radius = 1.0,
  Offset center = .zero,
}) {
  if (numVertices < 3) {
    throw ArgumentError("Circle must have at least three vertices.");
  }

  // Half of the angle between two adjacent vertices on the polygon.
  final theta = math.pi / numVertices;
  // Radius of the underlying RoundedPolygon object given the desired radius
  // of the circle.
  final polygonRadius = radius / math.cos(theta);
  return .new(
    numVertices,
    radius: polygonRadius,
    center: center,
    rounding: .new(radius: radius),
  );
}