From 964a968b36cd6462c8e91293cdb7654e4d25cde5 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 2 Mar 2014 17:43:44 -0500 Subject: [PATCH] Added center control point (basic functionality, just "stays inside" the ellipse for now but not working 100%) --- Shape.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Shape.h | 61 ++------------------------------------------------- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/Shape.cpp b/Shape.cpp index 74129b1..c33595b 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -386,3 +386,68 @@ bool Ellipse::includesPoint(qreal x, qreal y) return (horizontal*horizontal + vertical*vertical) <= 1; } +void Ellipse::setVertex(int i, const QPointF& v) +{ + // Save vertical axis vector. + const QVector2D& vAxis = getVerticalAxis(); + + // If changed one of the two rotation-controlling points, adjust the other two points. + if (i == 0 || i == 2) + { + // Change the vertex. + Shape::setVertex(i, v); + + // Retrieve the new horizontal axis vector and center. + const QVector2D& hAxis = getHorizontalAxis(); + const QVector2D center(getCenter()); + + // Ajust the two other points. + + // Compute a vector that is the 90 degree rotation of the horizontal axis, + // with a magnitude equal to the vertical radius. + QVector2D vAxisVec(hAxis.y(), -hAxis.x()); // 90 deg CW rotation of horiz. axis + vAxisVec.normalize(); + vAxisVec *= vAxis.length() / 2; + + // Assign vertical control points. + QPointF v1 = (center + vAxisVec).toPointF(); + QPointF v3 = (center - vAxisVec).toPointF(); + Shape::setVertex(1, v1); + Shape::setVertex(3, v3); + } + + // If changed one of the two other points, just change the vertical axis. + else if (i == 1 || i == 3) + { + // Retrieve the new horizontal axis vector and center. + const QVector2D center(getCenter()); + + QVector2D vFromCenter = QVector2D(v) - center; + + // Find projection of v onto vAxis / 2. + QVector2D vAxisNormalized = vAxis.normalized(); + const QVector2D& projection = QVector2D::dotProduct( vFromCenter, vAxisNormalized ) * vAxisNormalized; + + // Assign vertical control points. + QPointF v1; + QPointF v3; + if (i == 1) + { + v1 = (center + projection).toPointF(); + v3 = (center - projection).toPointF(); + } + else + { + v1 = (center - projection).toPointF(); + v3 = (center + projection).toPointF(); + } + Shape::setVertex(1, v1); + Shape::setVertex(3, v3); + } + + // Center control point (make sure it stays inside!). + else { + if (includesPoint(v)) + Shape::setVertex(i, v); + } +} diff --git a/Shape.h b/Shape.h index e08c701..c6274d2 100644 --- a/Shape.h +++ b/Shape.h @@ -218,6 +218,7 @@ public: _addVertex(p2); _addVertex(p3); _addVertex(p4); + _addVertex(getCenter()); // add a point in the center } virtual ~Ellipse() {} @@ -249,65 +250,6 @@ public: return QVector2D(getVertex(1)) - QVector2D(getVertex(3)); } - // Override the parent, checking to make sure the vertices are displaced correctly. - virtual void setVertex(int i, const QPointF& v) - { - // Save vertical axis vector. - const QVector2D& vAxis = getVerticalAxis(); - - // If changed one of the two rotation-controlling points, adjust the other two points. - if (i == 0 || i == 2) - { - // Change the vertex. - Shape::setVertex(i, v); - - // Retrieve the new horizontal axis vector and center. - const QVector2D& hAxis = getHorizontalAxis(); - const QVector2D center(getCenter()); - - // Ajust the two other points. - - // Compute a vector that is the 90 degree rotation of the horizontal axis, - // with a magnitude equal to the vertical radius. - QVector2D vAxisVec(hAxis.y(), -hAxis.x()); // 90 deg CW rotation of horiz. axis - vAxisVec.normalize(); - vAxisVec *= vAxis.length() / 2; - - // Assign vertical control points. - QPointF v1 = (center + vAxisVec).toPointF(); - QPointF v3 = (center - vAxisVec).toPointF(); - Shape::setVertex(1, v1); - Shape::setVertex(3, v3); - } - - // If changed one of the two other points, just change the vertical axis. - else - { - // Retrieve the new horizontal axis vector and center. - const QVector2D center(getCenter()); - - QVector2D vFromCenter = QVector2D(v) - center; - - // Find projection of v onto vAxis / 2. - QVector2D vAxisNormalized = vAxis.normalized(); - const QVector2D& projection = QVector2D::dotProduct( vFromCenter, vAxisNormalized ) * vAxisNormalized; - - // Assign vertical control points. - QPointF v1; - QPointF v3; - if (i == 1) - { - v1 = (center + projection).toPointF(); - v3 = (center - projection).toPointF(); - } - else - { - v1 = (center - projection).toPointF(); - v3 = (center + projection).toPointF(); - } - Shape::setVertex(1, v1); - Shape::setVertex(3, v3); - } qreal getHorizontalRadius() const { return getHorizontalAxis().length() / 2; } @@ -327,6 +269,7 @@ public: } // Override the parent, checking to make sure the vertices are displaced correctly. + virtual void setVertex(int i, const QPointF& v); //protected: // virtual void _vertexChanged(int i, Point* p=NULL) {