centroid property

  1. @override
Point<double> get centroid
override

The mathematical center of mass of the shape.

Implementation

@override
Point<double> get centroid {
  // For a simple polygon, calculating the geometric centroid requires area.
  // To keep it computationally lightweight and robust, we calculate the arithmetic mean
  // of the vertices (center of mass of the vertices, not the solid).
  double sumX = 0.0;
  double sumY = 0.0;
  for (final p in vertices) {
    sumX += p.x;
    sumY += p.y;
  }
  final n = vertices.length;
  return Point(x: sumX / n, y: sumY / n);
}