Calculate center or centroid of irregular shape or polygon in php function
by Prabakaran[ Edit ] 2012-04-25 12:41:03
<?php
$polygon = array(50,50, 75,25, 100,50, 125,75, 100,100, 75,125, 50,100, 25,75);
$er = polyCenter($polygon);
print_r($er);
function polyCenter($polygon){
//echo "<pre>";print_r($polygon);echo "</pre>";
$app = array_chunk($polygon,2);
//echo "<pre>";print_r($app);echo "</pre>";
$n = count($app);
for($i=0;$i<$n;$i++){
$x+=$app[$i][0];
//echo "<br>";
$y+=$app[$i][1];
}
$x = round($x/$n); $y = round($y/$n);
return $x.":".$y;
}
?>