From 45813562e266cf8e58ea9cb04f655bbb542a555e Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 31 May 2015 22:55:45 +0200 Subject: Make every line subclass editable --- polygon.cpp | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'polygon.cpp') diff --git a/polygon.cpp b/polygon.cpp index a292653..d8b71b7 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -3,21 +3,14 @@ Polygon::Polygon() : Line() { - m_polygon.append(QPoint(0,0)); - m_polygon.append(QPoint(0,0)); - m_polygon.append(QPoint(0,0)); + m_polygon.append(QPoint(10, 50)); + m_polygon.append(QPoint(30, 70)); + m_polygon.append(QPoint(50, 50)); } bool Polygon::isHit(const QPoint &clickPoint) { - QVector3D a(p2() - p1()); - QVector3D r1(p1()); - - QVector3D rq(clickPoint); - - float dist = rq.distanceToLine(r1, a.normalized()); - - return dist < 5; + return m_polygon.containsPoint(clickPoint, Qt::OddEvenFill); } void Polygon::draw(QPainter *painter) @@ -28,9 +21,9 @@ void Polygon::draw(QPainter *painter) penTemp.setColor(Qt::red); painter->setPen(penTemp); - painter->drawEllipse(p1(), 5, 5); - painter->drawEllipse(p2(), 5, 5); - painter->drawEllipse(QPoint(p1().rx() + 20, p1().ry() - 20), 5, 5); + painter->drawEllipse(m_polygon.point(0), 5, 5); + painter->drawEllipse(m_polygon.point(1), 5, 5); + painter->drawEllipse(m_polygon.point(2), 5, 5); } else { @@ -39,9 +32,33 @@ void Polygon::draw(QPainter *painter) painter->setPen(penNormal); } - m_polygon.replace(0, p1()); - m_polygon.replace(1, p2()); - m_polygon.replace(2, QPoint(p1().rx() + 20, p1().ry() - 20)); - painter->drawPolygon(m_polygon); } + + +void Polygon::move(const QPoint &oldPoint, const QPoint &newPoint) +{ + if(m_selected) + { + QPoint offset = newPoint - oldPoint; + QVector3D vecOld(oldPoint); + if(vecOld.distanceToPoint(QVector3D(m_polygon.point(0))) < 5) + { + m_polygon.replace(0, m_polygon.point(0) + offset); + } + else if(vecOld.distanceToPoint(QVector3D(m_polygon.point(1))) < 5) + { + m_polygon.replace(1, m_polygon.point(1) + offset); + } + else if(vecOld.distanceToPoint(QVector3D(m_polygon.point(2))) < 5) + { + m_polygon.replace(2, m_polygon.point(2) + offset); + } + else + { + m_polygon.replace(0, m_polygon.point(0) + offset); + m_polygon.replace(1, m_polygon.point(1) + offset); + m_polygon.replace(2, m_polygon.point(2) + offset); + } + } +} -- cgit v1.2.3-70-g09d2