Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
tumic0 committed Jun 24, 2024
1 parent f8deb57 commit 21da89c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
21 changes: 5 additions & 16 deletions src/map/IMG/rastertile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,22 @@ static int minShieldZoom(Shield::Type type)
}
}

static qreal area(const QVector<QPointF> &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<QPointF> &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);
}

Expand Down
21 changes: 5 additions & 16 deletions src/map/mapsforge/rastertile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 21da89c

Please sign in to comment.