Skip to content

Commit 857d544

Browse files
committed
Improve numerical stability of QgsAbstractGeometryV2::centroid
1 parent 149ab30 commit 857d544

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

‎src/core/geometry/qgsabstractgeometryv2.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ QgsPointV2 QgsAbstractGeometryV2::centroid() const
174174
double A = 0.;
175175
double Cx = 0.;
176176
double Cy = 0.;
177+
QgsPointV2 v0 = vertexAt( QgsVertexId( 0, 0, 0 ) );
177178
int i = 0, j = 1;
178179
if ( vertexAt( QgsVertexId( 0, 0, 0 ) ) != vertexAt( QgsVertexId( 0, 0, n - 1 ) ) )
179180
{
@@ -184,6 +185,10 @@ QgsPointV2 QgsAbstractGeometryV2::centroid() const
184185
{
185186
QgsPointV2 vi = vertexAt( QgsVertexId( 0, 0, i ) );
186187
QgsPointV2 vj = vertexAt( QgsVertexId( 0, 0, j ) );
188+
vi.rx() -= v0.x();
189+
vi.ry() -= v0.y();
190+
vj.rx() -= v0.x();
191+
vj.ry() -= v0.y();
187192
double d = vi.x() * vj.y() - vj.x() * vi.y();
188193
A += d;
189194
Cx += ( vi.x() + vj.x() ) * d;
@@ -203,7 +208,7 @@ QgsPointV2 QgsAbstractGeometryV2::centroid() const
203208
}
204209
else
205210
{
206-
return QgsPointV2( Cx / ( 3. * A ), Cy / ( 3. * A ) );
211+
return QgsPointV2( v0.x() + Cx / ( 3. * A ), v0.y() + Cy / ( 3. * A ) );
207212
}
208213
}
209214

0 commit comments

Comments
 (0)