diff --git a/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp b/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp index 5a42bf1f33d..6f9b34aa0c2 100644 --- a/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp +++ b/src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp @@ -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) { - if (intersectionLine) - *intersectionLine = polygonLine; - return true; + qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length(); + if (!found || dist < mindist) { + mindist = dist; + ipoint = point; + iline = polygonLine; + found = true; + } } } - return false; + if (found) { + if (intersectionPoint) + *intersectionPoint = ipoint; + if (intersectionLine) + *intersectionLine = iline; + } + return found; } namespace { diff --git a/src/libs/modelinglib/qmt/infrastructure/geometryutilities.h b/src/libs/modelinglib/qmt/infrastructure/geometryutilities.h index f0b4f697b0b..2ec1701aa09 100644 --- a/src/libs/modelinglib/qmt/infrastructure/geometryutilities.h +++ b/src/libs/modelinglib/qmt/infrastructure/geometryutilities.h @@ -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);