** Computes the center of gravity for a polygon based on it's largest outer ring only. */
| 1213 | ** Computes the center of gravity for a polygon based on it's largest outer ring only. |
| 1214 | */ |
| 1215 | static int getPolygonCenterOfGravity(shapeObj *p, pointObj *lp) |
| 1216 | { |
| 1217 | int i, j; |
| 1218 | double area=0; |
| 1219 | double sx=0, sy=0, tsx, tsy, s; /* sums */ |
| 1220 | double a; |
| 1221 | |
| 1222 | double largestArea=0; |
| 1223 | |
| 1224 | for(i=0; i<p->numlines; i++) { |
| 1225 | tsx = tsy = s = 0; /* reset the ring sums */ |
| 1226 | for(j=0; j<p->line[i].numpoints-1; j++) { |
| 1227 | a = p->line[i].point[j].x*p->line[i].point[j+1].y - p->line[i].point[j+1].x*p->line[i].point[j].y; |
| 1228 | s += a; |
| 1229 | tsx += (p->line[i].point[j].x + p->line[i].point[j+1].x)*a; |
| 1230 | tsy += (p->line[i].point[j].y + p->line[i].point[j+1].y)*a; |
| 1231 | } |
| 1232 | area = MS_ABS(s/2); |
| 1233 | |
| 1234 | if(area > largestArea) { |
| 1235 | largestArea = area; |
| 1236 | sx = s>0?tsx:-tsx; |
| 1237 | sy = s>0?tsy:-tsy; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | lp->x = sx/(6*largestArea); |
| 1242 | lp->y = sy/(6*largestArea); |
| 1243 | |
| 1244 | return MS_SUCCESS; |
| 1245 | } |
| 1246 | |
| 1247 | int msGetPolygonCentroid(shapeObj *p, pointObj *lp, double *miny, double *maxy) |
| 1248 | { |
no outgoing calls
no test coverage detected