Guest User

QGIS Core Centroid NEW

a guest
Mar 29th, 2012
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* /src/core/qgscentralpointpositionmanager.cpp */
  2. int QgsCentralPointPositionManager::calculatePolygonCentroid( double x[], double y[], int numberOfPoints, double& centroidX, double& centroidY ) const
  3. {
  4.   register int i, j;
  5.   double ai, atmp = 0, xtmp = 0, ytmp = 0;
  6.   if ( numberOfPoints < 3 )
  7.   {
  8.     return 1;
  9.   }
  10.  
  11.   for ( i = numberOfPoints - 1, j = 0; j < numberOfPoints; i = j, j++ )
  12.   {
  13.     ai = ( x[i] - x[0] ) * ( y[j] - y[0] ) - ( x[j] - x[0] ) * ( y[i] - y[0] );
  14.     atmp += ai;
  15.     xtmp += ( x[j] + x[i] - 2 * x[0] ) * ai;
  16.     ytmp += ( y[j] + y[i] - 2 * y[0] ) * ai;
  17.   }
  18.   if ( atmp == 0 )
  19.   {
  20.     return 2;
  21.   }
  22.   centroidX = x[0] + xtmp / ( 3 * atmp );
  23.   centroidY = y[0] + ytmp / ( 3 * atmp );
  24.   return 0;
  25. }
RAW Paste Data