Esri/terraformer

Centroid of polygon

Closed this issue · 1 comments

We should impliment a Terraformer.Tools.centroid formula.

From http://stackoverflow.com/a/9939071. The output of the also matches the ArcGIS Javascript SDKs centroid formula.

function get_polygon_centroid(pts){
  var twicearea=0,
      x=0, y=0,
      nPts = pts.length,
      p1, p2, f;

  for (var i=0, j=nPts-1 ;i<nPts;j=i++) {
    p1=pts[i]; p2=pts[j];
    twicearea+=p1[0]*p2[1];
    twicearea-=p1[1]*p2[0];
    f=p1[0]*p2[1]-p2[0]*p1[1];
    x+=(p1[0]+p2[0])*f;
    y+=(p1[1]+p2[1])*f;
  }
  f=twicearea*3;
  return [x/f,y/f];
}

closing, as turfjs already provides support for this.