1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Optimization

This commit is contained in:
Martin Tůma 2024-06-24 08:48:44 +02:00
parent f8deb573b0
commit 21da89cad1
2 changed files with 10 additions and 32 deletions

View File

@ -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) static QPointF centroid(const QVector<QPointF> &polygon)
{ {
qreal area = 0;
qreal cx = 0, cy = 0; qreal cx = 0, cy = 0;
qreal factor = 1.0 / (6.0 * area(polygon));
for (int i = 0; i < polygon.size(); i++) { 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() qreal f = (polygon.at(i).x() * polygon.at(j).y() - polygon.at(j).x()
* polygon.at(i).y()); * polygon.at(i).y());
area += f;
cx += (polygon.at(i).x() + polygon.at(j).x()) * f; cx += (polygon.at(i).x() + polygon.at(j).x()) * f;
cy += (polygon.at(i).y() + polygon.at(j).y()) * f; cy += (polygon.at(i).y() + polygon.at(j).y()) * f;
} }
qreal factor = 1.0 / (3.0 * area);
return QPointF(cx * factor, cy * factor); return QPointF(cx * factor, cy * factor);
} }

View File

@ -15,33 +15,22 @@ using namespace Mapsforge;
static double LIMIT = cos(deg2rad(170)); 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) static QPointF centroid(const QPainterPath &polygon)
{ {
qreal area = 0;
qreal cx = 0, cy = 0; qreal cx = 0, cy = 0;
qreal factor = 1.0 / (6.0 * area(polygon));
for (int i = 0; i < polygon.elementCount(); i++) { 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 qreal f = (polygon.elementAt(i).x * polygon.elementAt(j).y
- polygon.elementAt(j).x * polygon.elementAt(i).y); - polygon.elementAt(j).x * polygon.elementAt(i).y);
area += f;
cx += (polygon.elementAt(i).x + polygon.elementAt(j).x) * f; cx += (polygon.elementAt(i).x + polygon.elementAt(j).x) * f;
cy += (polygon.elementAt(i).y + polygon.elementAt(j).y) * f; cy += (polygon.elementAt(i).y + polygon.elementAt(j).y) * f;
} }
qreal factor = 1.0 / (3.0 * area);
return QPointF(cx * factor, cy * factor); return QPointF(cx * factor, cy * factor);
} }