Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* /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] * y[j] - x[j] * y[i];
- atmp += ai;
- xtmp += ( x[j] + x[i] ) * ai;
- ytmp += ( y[j] + y[i] ) * ai;
- }
- if ( atmp == 0 )
- {
- return 2;
- }
- centroidX = xtmp / ( 3 * atmp );
- centroidY = ytmp / ( 3 * atmp );
- return 0;
- }
- /* /src/core/pal/pointset.cpp */
- void PointSet::getCentroid( double &px, double &py )
- {
- // for explanation see this page:
- // http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
- int i, j;
- double cx = 0, cy = 0, A = 0, tmp;
- 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;
- A += tmp;
- }
- px = cx / ( 3 * A );
- py = cy / ( 3 * A );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement