Skip to content
Permalink
Browse files

pal: replaced getCentroid function with much faster routine that actu…

…ally calculates _real_ centroid.

Fixes also the problem with completely misplaced labels.


git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11007 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Jul 2, 2009
1 parent 8f7af1c commit 2ba94c2ba48662855857e0fe7d59565ca98ba7aa
Showing with 13 additions and 21 deletions.
  1. +13 −21 src/core/pal/pointset.cpp
@@ -1729,30 +1729,22 @@ namespace pal

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

0 comments on commit 2ba94c2

Please sign in to comment.
You can’t perform that action at this time.