diff options
author | Jochen Becher <jochen_becher@gmx.de> | 2019-11-14 21:37:23 +0100 |
---|---|---|
committer | Jochen Becher <jochen_becher@gmx.de> | 2020-02-23 15:54:52 +0000 |
commit | 87d2df9d7bb80bd323e68edd1c2d9142f78b2e62 (patch) | |
tree | e9abaad60be2c5b889d7e32ea08188772b15060f /src/libs/modelinglib | |
parent | db90ce21c9182468b09040cb60d1857957bfaffe (diff) |
modeleditor: Improve intersection of line with polygon
Change-Id: Ie546801fadedffaef24c53ce83ce0a039e77a04a
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Diffstat (limited to 'src/libs/modelinglib')
-rw-r--r-- | src/libs/modelinglib/qmt/infrastructure/geometryutilities.cpp | 30 | ||||
-rw-r--r-- | src/libs/modelinglib/qmt/infrastructure/geometryutilities.h | 3 |
2 files changed, 25 insertions, 8 deletions
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); |