Skip to content

Commit 5c29413

Browse files
committed
Labeling centroid calculation update (provided by Serge Dikiy)
1 parent 1d435a5 commit 5c29413

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/core/pal/pointset.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,18 +962,28 @@ namespace pal
962962
// http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
963963

964964
int i, j;
965-
double cx = 0, cy = 0, A = 0, tmp;
965+
double cx = 0, cy = 0, A = 0, tmp, sumx = 0, sumy = 0;
966966
for ( i = 0; i < nbPoints; i++ )
967967
{
968968
j = i + 1; if ( j == nbPoints ) j = 0;
969-
tmp = ( x[i] * y[j] - x[j] * y[i] );
970-
cx += ( x[i] + x[j] ) * tmp;
971-
cy += ( y[i] + y[j] ) * tmp;
969+
tmp = (( x[i] - x[0] ) * ( y[j] - y[0] ) - ( x[j] - x[0] ) * ( y[i] - y[0] ) );
970+
cx += ( x[i] + x[j] - 2 * x[0] ) * tmp;
971+
cy += ( y[i] + y[j] - 2 * y[0] ) * tmp;
972972
A += tmp;
973+
sumx += x[i];
974+
sumy += y[i];
973975
}
974976

975-
px = cx / ( 3 * A );
976-
py = cy / ( 3 * A );
977+
if ( A == 0 )
978+
{
979+
px = sumx / nbPoints;
980+
py = sumy / nbPoints;
981+
}
982+
else
983+
{
984+
px = cx / ( 3 * A ) + x[0];
985+
py = cy / ( 3 * A ) + y[0];
986+
}
977987
}
978988

979989
} // end namespace

0 commit comments

Comments
 (0)