modeleditor: Improve intersection of line with polygon

Change-Id: Ie546801fadedffaef24c53ce83ce0a039e77a04a
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Jochen Becher
2019-11-14 21:37:23 +01:00
parent db90ce21c9
commit 87d2df9d7b
2 changed files with 25 additions and 8 deletions

View File

@@ -54,22 +54,38 @@ QLineF GeometryUtilities::stretch(const QLineF &line, double p1Extension, double
}
bool GeometryUtilities::intersect(const QPolygonF &polygon, const QLineF &line,
QPointF *intersectionPoint, QLineF *intersectionLine)
QPointF *intersectionPoint, QLineF *intersectionLine,
int nearestPoint)
{
bool found = false;
qreal mindist = 0;
QPointF ipoint;
QLineF iline;
for (int i = 0; i <= polygon.size() - 2; ++i) {
QLineF polygonLine(polygon.at(i), polygon.at(i+1));
QPointF point;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QLineF::IntersectType intersectionType = polygonLine.intersect(line, intersectionPoint);
QLineF::IntersectType intersectionType = polygonLine.intersect(line, &point);
#else
QLineF::IntersectType intersectionType = polygonLine.intersects(line, intersectionPoint);
QLineF::IntersectType intersectionType = polygonLine.intersects(line, &point);
#endif
if (intersectionType == QLineF::BoundedIntersection) {
qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length();
if (!found || dist < mindist) {
mindist = dist;
ipoint = point;
iline = polygonLine;
found = true;
}
}
}
if (found) {
if (intersectionPoint)
*intersectionPoint = ipoint;
if (intersectionLine)
*intersectionLine = polygonLine;
return true;
*intersectionLine = iline;
}
}
return false;
return found;
}
namespace {

View File

@@ -52,7 +52,8 @@ public:
static QLineF stretch(const QLineF &line, double p1Extension, double p2Extension);
static bool intersect(const QPolygonF &polygon, const QLineF &line,
QPointF *intersectionPoint, QLineF *intersectionLine = nullptr);
QPointF *intersectionPoint = nullptr, QLineF *intersectionLine = nullptr,
int nearestPoint = 1);
static bool placeRectAtLine(const QRectF &rect, const QLineF &line, double lineOffset,
double distance, const QLineF &intersectionLine, QPointF *placement,
Side *horizontalAlignedSide);