
QGIS Core Centroid NEW
By: a guest on
Mar 29th, 2012 | syntax:
C++ | size: 0.73 KB | hits: 59 | expires: Never
/* /src/core/qgscentralpointpositionmanager.cpp */
int QgsCentralPointPositionManager::calculatePolygonCentroid( double x[], double y[], int numberOfPoints, double& centroidX, double& centroidY ) const
{
register int i, j;
double ai, atmp = 0, xtmp = 0, ytmp = 0;
if ( numberOfPoints < 3 )
{
return 1;
}
for ( i = numberOfPoints - 1, j = 0; j < numberOfPoints; i = j, j++ )
{
ai = ( x[i] - x[0] ) * ( y[j] - y[0] ) - ( x[j] - x[0] ) * ( y[i] - y[0] );
atmp += ai;
xtmp += ( x[j] + x[i] - 2 * x[0] ) * ai;
ytmp += ( y[j] + y[i] - 2 * y[0] ) * ai;
}
if ( atmp == 0 )
{
return 2;
}
centroidX = x[0] + xtmp / ( 3 * atmp );
centroidY = y[0] + ytmp / ( 3 * atmp );
return 0;
}