mirror of
https://github.com/mapmapteam/mapmap.git
synced 2026-06-16 12:33:19 +02:00
Use the containsPoint() method of polygon to test point inclusion inside shape.
This commit is contained in:
@@ -21,40 +21,40 @@
|
||||
|
||||
bool Shape::includesPoint(int x, int y)
|
||||
{
|
||||
Point *prev = NULL, *cur;
|
||||
int left = 0, right = 0, maxy, miny;
|
||||
for (std::vector<Point*>::iterator it = vertices.begin() ;
|
||||
it != vertices.end(); it++)
|
||||
{
|
||||
if (!prev) {
|
||||
prev = vertices.back();
|
||||
}
|
||||
cur = *it;
|
||||
miny = std::min(cur->y(), prev->y());
|
||||
maxy = std::max(cur->y(), prev->y());
|
||||
|
||||
if (y > miny && y < maxy) {
|
||||
if (prev->x() == cur->x())
|
||||
{
|
||||
if (x < cur->x())
|
||||
right++;
|
||||
else left++;
|
||||
}
|
||||
else
|
||||
{
|
||||
double slope = (cur->y() - prev->y()) / (cur->x() - prev->x());
|
||||
double offset = cur->y() - slope * cur->x();
|
||||
int xintersect = int((y - offset ) / slope);
|
||||
if (x < xintersect)
|
||||
right++;
|
||||
else left++;
|
||||
}
|
||||
}
|
||||
prev = *it;
|
||||
}
|
||||
if (right % 2 && left % 2)
|
||||
return true;
|
||||
return false;
|
||||
return toPolygon().containsPoint(QPointF(x, y), Qt::OddEvenFill);
|
||||
// QVector<QPointF>::iterator prev;
|
||||
// int left = 0, right = 0, maxy, miny;
|
||||
// for (QVector<QPointF>::iterator it = vertices.begin() ;
|
||||
// it != vertices.end(); it++)
|
||||
// {
|
||||
// if (!prev) {
|
||||
// prev = vertices.back();
|
||||
// }
|
||||
// miny = qMin(it->y(), prev->y());
|
||||
// maxy = qMax(it->y(), prev->y());
|
||||
//
|
||||
// if (y > miny && y < maxy) {
|
||||
// if (prev->x() == it->x())
|
||||
// {
|
||||
// if (x < it->x())
|
||||
// right++;
|
||||
// else left++;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// double slope = (it->y() - prev->y()) / (it->x() - prev->x());
|
||||
// double offset = it->y() - slope * it->x();
|
||||
// int xintersect = int((y - offset ) / slope);
|
||||
// if (x < xintersect)
|
||||
// right++;
|
||||
// else left++;
|
||||
// }
|
||||
// }
|
||||
// prev = *it;
|
||||
// }
|
||||
// if (right % 2 && left % 2)
|
||||
// return true;
|
||||
// return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user