MCPcopy Create free account
hub / github.com/MapServer/MapServer / msGetPolygonCentroid

Function msGetPolygonCentroid

mapprimitive.c:1247–1272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1245}
1246
1247int msGetPolygonCentroid(shapeObj *p, pointObj *lp, double *miny, double *maxy)
1248{
1249 int i,j;
1250 double cent_weight_x=0.0, cent_weight_y=0.0;
1251 double len, total_len=0;
1252
1253 *miny = *maxy = p->line[0].point[0].y;
1254 for(i=0; i<p->numlines; i++) {
1255 for(j=1; j<p->line[i].numpoints; j++) {
1256 *miny = MS_MIN(*miny, p->line[i].point[j].y);
1257 *maxy = MS_MAX(*maxy, p->line[i].point[j].y);
1258 len = msDistancePointToPoint(&(p->line[i].point[j-1]), &(p->line[i].point[j]));
1259 cent_weight_x += len * ((p->line[i].point[j-1].x + p->line[i].point[j].x)/2);
1260 cent_weight_y += len * ((p->line[i].point[j-1].y + p->line[i].point[j].y)/2);
1261 total_len += len;
1262 }
1263 }
1264
1265 if(total_len == 0)
1266 return(MS_FAILURE);
1267
1268 lp->x = cent_weight_x / total_len;
1269 lp->y = cent_weight_y / total_len;
1270
1271 return(MS_SUCCESS);
1272}
1273
1274/*
1275** Find a label point in a polygon.

Callers 2

msDrawTransformedShapeFunction · 0.85
msPolygonLabelPointFunction · 0.85

Calls 1

msDistancePointToPointFunction · 0.85

Tested by

no test coverage detected