Skip to content
Permalink
Browse files

Labeling centroid calculation update (provided by Serge Dikiy)

  • Loading branch information
dakcarto committed Sep 24, 2012
1 parent 1d435a5 commit 5c294139ddc368f74d5d7dc6668d72d2a8633499
Showing with 16 additions and 6 deletions.
  1. +16 −6 src/core/pal/pointset.cpp
@@ -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

0 comments on commit 5c29413

Please sign in to comment.
You can’t perform that action at this time.