From d211a9deb76d6def4d46a973d1d6061223698dd6 Mon Sep 17 00:00:00 2001 From: dismine Date: Wed, 29 Jan 2014 11:18:26 +0200 Subject: [PATCH] Which way is better? See VAbstractTool::ClosestPoint. --HG-- branch : develop --- src/tools/drawTools/vtoolheight.cpp | 17 +------------ src/tools/vabstracttool.cpp | 39 +++++++++++------------------ src/tools/vabstracttool.h | 2 +- 3 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/tools/drawTools/vtoolheight.cpp b/src/tools/drawTools/vtoolheight.cpp index 7e0b59afa..3548a93b3 100644 --- a/src/tools/drawTools/vtoolheight.cpp +++ b/src/tools/drawTools/vtoolheight.cpp @@ -115,24 +115,9 @@ void VToolHeight::Create(const qint64 _id, const QString &pointName, const QStri } } -//TODO Which way is better? See VAbstractTool::ClosestPoint. QPointF VToolHeight::FindPoint(const QLineF &line, const QPointF &point) { - qreal a = 0, b = 0, c = 0; - LineCoefficients(line, &a, &b, &c); - qreal x = point.x() + a; - qreal y = b + point.y(); - QLineF lin (point, QPointF(x, y)); - QPointF p; - QLineF::IntersectType intersect = line.intersect(lin, &p); - if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection) - { - return p; - } - else - { - return QPointF(); - } + return VAbstractTool::ClosestPoint(line, point); } void VToolHeight::FullUpdateFromFile() diff --git a/src/tools/vabstracttool.cpp b/src/tools/vabstracttool.cpp index 26af3c6c8..5e443972a 100644 --- a/src/tools/vabstracttool.cpp +++ b/src/tools/vabstracttool.cpp @@ -86,18 +86,18 @@ void VAbstractTool::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view) QRectF rec1; if (t.m11() < 1) { - rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22()); + qreal width = rec0.width()/t.m11(); + qreal height = rec0.height()/t.m22(); + rec1 = QRect(0, 0, static_cast(width), static_cast(height)); rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y()); QPolygonF polygone = view->mapToScene(rec1.toRect()); rec1 = polygone.boundingRect(); - } else { rec1 = rec0; } - rec1 = rec1.united(rect.toRect()); sc->setSceneRect(rec1); } @@ -134,7 +134,6 @@ QPointF VAbstractTool::LineIntersectRect(QRectF rec, QLineF line) qint32 VAbstractTool::LineIntersectCircle(const QPointF ¢er, qreal radius, const QLineF &line, QPointF &p1, QPointF &p2) { - const qreal eps = 1e-8; //coefficient for equation of segment qreal a = 0, b = 0, c = 0; LineCoefficients(line, &a, &b, &c); @@ -143,7 +142,7 @@ qint32 VAbstractTool::LineIntersectCircle(const QPointF ¢er, qreal radius, c // how many solutions? qint32 flag = 0; qreal d = QLineF (center, p).length(); - if (qAbs (d - radius) <= eps) + if (qFuzzyCompare(d, radius)) { flag = 1; } @@ -167,31 +166,23 @@ qint32 VAbstractTool::LineIntersectCircle(const QPointF ¢er, qreal radius, c return flag; } -QPointF VAbstractTool::ClosestPoint(const QLineF &line, const QPointF &p) +QPointF VAbstractTool::ClosestPoint(const QLineF &line, const QPointF &point) { - QLineF lineP2pointFrom = QLineF(line.p2(), p); - //right triangle always have one angle with 90 degree - //Now we want find angle between projection and line from p. - qreal angle = 180-(line.angleTo(lineP2pointFrom)-90); - //Swap first and last points line. Need for rotation. - QLineF pointFromlineP2 = QLineF(p, line.p2()); - pointFromlineP2.setAngle(pointFromlineP2.angle()+angle); - //After rotation we will have two intersect lines. Left just find intersect point. - QPointF point; - QLineF::IntersectType type = pointFromlineP2.intersect(line, &point); - if ( type == QLineF::BoundedIntersection ) + qreal a = 0, b = 0, c = 0; + LineCoefficients(line, &a, &b, &c); + qreal x = point.x() + a; + qreal y = b + point.y(); + QLineF lin (point, QPointF(x, y)); + QPointF p; + QLineF::IntersectType intersect = line.intersect(lin, &p); + if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection) { - return point; + return p; } else { - if ( type == QLineF::NoIntersection || type == QLineF::UnboundedIntersection ) - { - Q_ASSERT_X(type != QLineF::BoundedIntersection, Q_FUNC_INFO, "Don't have point of intersection."); - return point; - } + return QPointF(); } - return point; } QPointF VAbstractTool::addVector(const QPointF &p, const QPointF &p1, const QPointF &p2, qreal k) diff --git a/src/tools/vabstracttool.h b/src/tools/vabstracttool.h index 155145ee6..3d8eee4be 100644 --- a/src/tools/vabstracttool.h +++ b/src/tools/vabstracttool.h @@ -78,7 +78,7 @@ public: * @param p point. * @return point on line or extended line if origin size too small. */ - static QPointF ClosestPoint(const QLineF &line, const QPointF &p); + static QPointF ClosestPoint(const QLineF &line, const QPointF &point); /** * @brief addVector * @param p