mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-11-28 05:34:47 +01:00
Fixed centroid computation
This commit is contained in:
parent
8a9b7dc7ec
commit
9538d15d79
@ -59,10 +59,11 @@ static double area(const QVector<Coordinates> &polygon)
|
|||||||
{
|
{
|
||||||
double area = 0;
|
double area = 0;
|
||||||
|
|
||||||
for (int i = 0; i < polygon.size(); i++) {
|
for (int i = 0; i < polygon.size() - 1; i++) {
|
||||||
int j = (i + 1) % polygon.size();
|
const Coordinates &pi = polygon.at(i);
|
||||||
area += polygon.at(i).lon() * polygon.at(j).lat();
|
const Coordinates &pj = polygon.at(i+1);
|
||||||
area -= polygon.at(i).lat() * polygon.at(j).lon();
|
area += pi.lon() * pj.lat();
|
||||||
|
area -= pi.lat() * pj.lon();
|
||||||
}
|
}
|
||||||
area /= 2.0;
|
area /= 2.0;
|
||||||
|
|
||||||
@ -71,15 +72,18 @@ static double area(const QVector<Coordinates> &polygon)
|
|||||||
|
|
||||||
static Coordinates centroid(const QVector<Coordinates> &polygon)
|
static Coordinates centroid(const QVector<Coordinates> &polygon)
|
||||||
{
|
{
|
||||||
|
Q_ASSERT(polygon.size() > 3);
|
||||||
|
Q_ASSERT(polygon.first() == polygon.last());
|
||||||
|
|
||||||
double cx = 0, cy = 0;
|
double cx = 0, cy = 0;
|
||||||
double factor = 1.0 / (6.0 * area(polygon));
|
double factor = 1.0 / (6.0 * area(polygon));
|
||||||
|
|
||||||
for (int i = 0; i < polygon.size(); i++) {
|
for (int i = 0; i < polygon.size() - 1; i++) {
|
||||||
int j = (i + 1) % polygon.size();
|
const Coordinates &pi = polygon.at(i);
|
||||||
qreal f = (polygon.at(i).lon() * polygon.at(j).lat()
|
const Coordinates &pj = polygon.at(i+1);
|
||||||
- polygon.at(j).lon() * polygon.at(i).lat());
|
double f = (pi.lon() * pj.lat() - pj.lon() * pi.lat());
|
||||||
cx += (polygon.at(i).lon() + polygon.at(j).lon()) * f;
|
cx += (pi.lon() + pj.lon()) * f;
|
||||||
cy += (polygon.at(i).lat() + polygon.at(j).lat()) * f;
|
cy += (pi.lat() + pj.lat()) * f;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Coordinates(cx * factor, cy * factor);
|
return Coordinates(cx * factor, cy * factor);
|
||||||
|
Loading…
Reference in New Issue
Block a user