diff --git a/src/map/IMG/rastertile.cpp b/src/map/IMG/rastertile.cpp index d88813d2..1b9e31b8 100644 --- a/src/map/IMG/rastertile.cpp +++ b/src/map/IMG/rastertile.cpp @@ -70,33 +70,22 @@ static int minShieldZoom(Shield::Type type) } } -static qreal area(const QVector &polygon) -{ - qreal area = 0; - - for (int i = 0; i < polygon.size(); i++) { - int j = (i + 1) % polygon.size(); - area += polygon.at(i).x() * polygon.at(j).y(); - area -= polygon.at(i).y() * polygon.at(j).x(); - } - area /= 2.0; - - return area; -} - static QPointF centroid(const QVector &polygon) { + qreal area = 0; qreal cx = 0, cy = 0; - qreal factor = 1.0 / (6.0 * area(polygon)); for (int i = 0; i < polygon.size(); i++) { - int j = (i + 1) % polygon.size(); + int j = (i == polygon.size() - 1) ? 0 : i + 1; qreal f = (polygon.at(i).x() * polygon.at(j).y() - polygon.at(j).x() * polygon.at(i).y()); + area += f; cx += (polygon.at(i).x() + polygon.at(j).x()) * f; cy += (polygon.at(i).y() + polygon.at(j).y()) * f; } + qreal factor = 1.0 / (3.0 * area); + return QPointF(cx * factor, cy * factor); } diff --git a/src/map/mapsforge/rastertile.cpp b/src/map/mapsforge/rastertile.cpp index bd44fd3e..02eb723f 100644 --- a/src/map/mapsforge/rastertile.cpp +++ b/src/map/mapsforge/rastertile.cpp @@ -15,33 +15,22 @@ using namespace Mapsforge; static double LIMIT = cos(deg2rad(170)); -static qreal area(const QPainterPath &polygon) -{ - qreal area = 0; - - for (int i = 0; i < polygon.elementCount(); i++) { - int j = (i + 1) % polygon.elementCount(); - area += polygon.elementAt(i).x * polygon.elementAt(j).y; - area -= polygon.elementAt(i).y * polygon.elementAt(j).x; - } - area /= 2.0; - - return area; -} - static QPointF centroid(const QPainterPath &polygon) { + qreal area = 0; qreal cx = 0, cy = 0; - qreal factor = 1.0 / (6.0 * area(polygon)); for (int i = 0; i < polygon.elementCount(); i++) { - int j = (i + 1) % polygon.elementCount(); + int j = (i == polygon.elementCount() - 1) ? 0 : i + 1; qreal f = (polygon.elementAt(i).x * polygon.elementAt(j).y - polygon.elementAt(j).x * polygon.elementAt(i).y); + area += f; cx += (polygon.elementAt(i).x + polygon.elementAt(j).x) * f; cy += (polygon.elementAt(i).y + polygon.elementAt(j).y) * f; } + qreal factor = 1.0 / (3.0 * area); + return QPointF(cx * factor, cy * factor); }