[QGIS Commit] r11007 - branches/symbology-ng-branch/src/core/pal
svn_qgis at osgeo.org
svn_qgis at osgeo.org
Thu Jul 2 11:14:33 EDT 2009
Author: wonder
Date: 2009-07-02 11:14:33 -0400 (Thu, 02 Jul 2009)
New Revision: 11007
Modified:
branches/symbology-ng-branch/src/core/pal/pointset.cpp
Log:
pal: replaced getCentroid function with much faster routine that actually calculates _real_ centroid.
Fixes also the problem with completely misplaced labels.
Modified: branches/symbology-ng-branch/src/core/pal/pointset.cpp
===================================================================
--- branches/symbology-ng-branch/src/core/pal/pointset.cpp 2009-07-02 11:05:23 UTC (rev 11006)
+++ branches/symbology-ng-branch/src/core/pal/pointset.cpp 2009-07-02 15:14:33 UTC (rev 11007)
@@ -1729,30 +1729,22 @@
void PointSet::getCentroid( double &px, double &py )
{
- double ix, iy;
- double mesh = min(( xmax - xmin ) / 10, ( ymax - ymin ) / 10 );
- bool ptOk = false;
+ // for explanation see this page:
+ // http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
- while ( !ptOk )
+ int i,j;
+ double cx=0, cy=0, A=0, tmp;
+ for ( i = 0; i < nbPoints; i++ )
{
- double best = -DBL_MAX;
- double dist;
- for ( ix = xmin;ix < xmax;ix = ix + mesh )
- {
- for ( iy = ymin;iy < ymax;iy = iy + mesh )
- {
- dist = getDist( ix, iy, NULL, NULL );
- if ( dist > 0 && dist > best )
- {
- ptOk = true;
- best = dist;
- px = ix;
- py = iy;
- }
- }
- }
- mesh /= 10;
+ 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);
}
} // end namespace
More information about the QGIS-commit
mailing list