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, 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) { for (int i = 0; i <= polygon.size() - 2; ++i) {
QLineF polygonLine(polygon.at(i), polygon.at(i+1)); QLineF polygonLine(polygon.at(i), polygon.at(i+1));
QPointF point;
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QLineF::IntersectType intersectionType = polygonLine.intersect(line, intersectionPoint); QLineF::IntersectType intersectionType = polygonLine.intersect(line, &point);
#else #else
QLineF::IntersectType intersectionType = polygonLine.intersects(line, intersectionPoint); QLineF::IntersectType intersectionType = polygonLine.intersects(line, &point);
#endif #endif
if (intersectionType == QLineF::BoundedIntersection) { if (intersectionType == QLineF::BoundedIntersection) {
if (intersectionLine) qreal dist = QLineF(point, nearestPoint <= 0 ? line.p1() : line.p2()).length();
*intersectionLine = polygonLine; if (!found || dist < mindist) {
return true; mindist = dist;
ipoint = point;
iline = polygonLine;
found = true;
}
} }
} }
return false; if (found) {
if (intersectionPoint)
*intersectionPoint = ipoint;
if (intersectionLine)
*intersectionLine = iline;
}
return found;
} }
namespace { namespace {

View File

@@ -52,7 +52,8 @@ public:
static QLineF stretch(const QLineF &line, double p1Extension, double p2Extension); static QLineF stretch(const QLineF &line, double p1Extension, double p2Extension);
static bool intersect(const QPolygonF &polygon, const QLineF &line, 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, static bool placeRectAtLine(const QRectF &rect, const QLineF &line, double lineOffset,
double distance, const QLineF &intersectionLine, QPointF *placement, double distance, const QLineF &intersectionLine, QPointF *placement,
Side *horizontalAlignedSide); Side *horizontalAlignedSide);