public function getCentroid()
{
$return = null;
if (count($this->points) >= 3) {
$centroid = new Point(0, 0);
$signedArea = 0;
$x0 = $y0 = $x1 = $y1 = 0;
$a = 0;
// Partial signed area
$vertices = $this->points;
$vertices[] = $vertices[0];
// For all vertices except last
for ($i = 0; $i < count($vertices) - 1; $i++) {
$x0 = $vertices[$i]->x;
$y0 = $vertices[$i]->y;
$x1 = $vertices[$i + 1]->x;
$y1 = $vertices[$i + 1]->y;
$a = $x0 * $y1 - $x1 * $y0;
$signedArea += $a;
$centroid->x += ($x0 + $x1) * $a;
$centroid->y += ($y0 + $y1) * $a;
}
$signedArea *= 0.5;
$centroid->x /= 6 * $signedArea;
$centroid->y /= 6 * $signedArea;
$return = $centroid;
}
return $return;
}