Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Labeling centroid calculation update (provided by Serge Dikiy)
  • Loading branch information
dakcarto committed Sep 24, 2012
1 parent 1d435a5 commit 5c29413
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/core/pal/pointset.cpp
Expand Up @@ -962,18 +962,28 @@ namespace pal
// http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/

int i, j;
double cx = 0, cy = 0, A = 0, tmp;
double cx = 0, cy = 0, A = 0, tmp, sumx = 0, sumy = 0;
for ( i = 0; i < nbPoints; i++ )
{
j = i + 1; if ( j == nbPoints ) j = 0;
tmp = ( x[i] * y[j] - x[j] * y[i] );
cx += ( x[i] + x[j] ) * tmp;
cy += ( y[i] + y[j] ) * tmp;
tmp = (( x[i] - x[0] ) * ( y[j] - y[0] ) - ( x[j] - x[0] ) * ( y[i] - y[0] ) );
cx += ( x[i] + x[j] - 2 * x[0] ) * tmp;
cy += ( y[i] + y[j] - 2 * y[0] ) * tmp;
A += tmp;
sumx += x[i];
sumy += y[i];
}

px = cx / ( 3 * A );
py = cy / ( 3 * A );
if ( A == 0 )
{
px = sumx / nbPoints;
py = sumy / nbPoints;
}
else
{
px = cx / ( 3 * A ) + x[0];
py = cy / ( 3 * A ) + y[0];
}
}

} // end namespace
Expand Down

0 comments on commit 5c29413

Please sign in to comment.