Use the containsPoint() method of polygon to test point inclusion inside shape.

This commit is contained in:
Tats
2014-02-09 01:03:16 -05:00
parent e36cb1a991
commit 89a5ce8787
+34 -34
View File
@@ -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;
}