Advertisement
cdsatrian

php centroid polygon

Sep 7th, 2012
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.42 KB | None | 0 0
  1. <?php
  2. //-- array polygon
  3. $polygon = array(array(50,50), array(75,25), array(105,55), array(125,75), array(100,100), array(75,125), array(50,100), array(25,75));
  4. $er = polyCenter($polygon);
  5. print_r($er);
  6.  
  7. function polyCenter($polygon){
  8.   $x=$y=0;
  9.   $n = count($polygon);
  10.   for($i=0;$i<$n;$i++){
  11.     $x+=$polygon[$i][0];
  12.     $y+=$polygon[$i][1];
  13.   }
  14.   $x = round($x/$n);
  15.   $y = round($y/$n);
  16.   return $x.":".$y;
  17. }
  18. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement