Which way is better? See VAbstractTool::ClosestPoint.

--HG--
branch : develop
This commit is contained in:
dismine 2014-01-29 11:18:26 +02:00
parent f5e0e5b2a0
commit d211a9deb7
3 changed files with 17 additions and 41 deletions

View file

@ -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) QPointF VToolHeight::FindPoint(const QLineF &line, const QPointF &point)
{ {
qreal a = 0, b = 0, c = 0; return VAbstractTool::ClosestPoint(line, point);
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();
}
} }
void VToolHeight::FullUpdateFromFile() void VToolHeight::FullUpdateFromFile()

View file

@ -86,18 +86,18 @@ void VAbstractTool::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view)
QRectF rec1; QRectF rec1;
if (t.m11() < 1) 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<qint32>(width), static_cast<qint32>(height));
rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y()); rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y());
QPolygonF polygone = view->mapToScene(rec1.toRect()); QPolygonF polygone = view->mapToScene(rec1.toRect());
rec1 = polygone.boundingRect(); rec1 = polygone.boundingRect();
} }
else else
{ {
rec1 = rec0; rec1 = rec0;
} }
rec1 = rec1.united(rect.toRect()); rec1 = rec1.united(rect.toRect());
sc->setSceneRect(rec1); sc->setSceneRect(rec1);
} }
@ -134,7 +134,6 @@ QPointF VAbstractTool::LineIntersectRect(QRectF rec, QLineF line)
qint32 VAbstractTool::LineIntersectCircle(const QPointF &center, qreal radius, const QLineF &line, QPointF &p1, qint32 VAbstractTool::LineIntersectCircle(const QPointF &center, qreal radius, const QLineF &line, QPointF &p1,
QPointF &p2) QPointF &p2)
{ {
const qreal eps = 1e-8;
//coefficient for equation of segment //coefficient for equation of segment
qreal a = 0, b = 0, c = 0; qreal a = 0, b = 0, c = 0;
LineCoefficients(line, &a, &b, &c); LineCoefficients(line, &a, &b, &c);
@ -143,7 +142,7 @@ qint32 VAbstractTool::LineIntersectCircle(const QPointF &center, qreal radius, c
// how many solutions? // how many solutions?
qint32 flag = 0; qint32 flag = 0;
qreal d = QLineF (center, p).length(); qreal d = QLineF (center, p).length();
if (qAbs (d - radius) <= eps) if (qFuzzyCompare(d, radius))
{ {
flag = 1; flag = 1;
} }
@ -167,32 +166,24 @@ qint32 VAbstractTool::LineIntersectCircle(const QPointF &center, qreal radius, c
return flag; 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); qreal a = 0, b = 0, c = 0;
//right triangle always have one angle with 90 degree LineCoefficients(line, &a, &b, &c);
//Now we want find angle between projection and line from p. qreal x = point.x() + a;
qreal angle = 180-(line.angleTo(lineP2pointFrom)-90); qreal y = b + point.y();
//Swap first and last points line. Need for rotation. QLineF lin (point, QPointF(x, y));
QLineF pointFromlineP2 = QLineF(p, line.p2()); QPointF p;
pointFromlineP2.setAngle(pointFromlineP2.angle()+angle); QLineF::IntersectType intersect = line.intersect(lin, &p);
//After rotation we will have two intersect lines. Left just find intersect point. if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection)
QPointF point;
QLineF::IntersectType type = pointFromlineP2.intersect(line, &point);
if ( type == QLineF::BoundedIntersection )
{ {
return point; return p;
} }
else else
{ {
if ( type == QLineF::NoIntersection || type == QLineF::UnboundedIntersection ) return QPointF();
{
Q_ASSERT_X(type != QLineF::BoundedIntersection, Q_FUNC_INFO, "Don't have point of intersection.");
return point;
} }
} }
return point;
}
QPointF VAbstractTool::addVector(const QPointF &p, const QPointF &p1, const QPointF &p2, qreal k) QPointF VAbstractTool::addVector(const QPointF &p, const QPointF &p1, const QPointF &p2, qreal k)
{ {

View file

@ -78,7 +78,7 @@ public:
* @param p point. * @param p point.
* @return point on line or extended line if origin size too small. * @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 * @brief addVector
* @param p * @param p