Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Add a function to move a point along a line
Agata Cacko71300c1ef090
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cppindex 86a6c37082..2af386e0fe 100644--- a/libs/global/kis_algebra_2d.cpp+++ b/libs/global/kis_algebra_2d.cpp@@ -122,6 +122,7 @@ qreal directionBetweenPoints(const QPointF &p1, const QPointF &p2, qreal default}const QVector2D diff(p2 - p1);+ //qCritical() << ""return std::atan2(diff.y(), diff.x());}@@ -1795,11 +1796,53 @@ QList<QPainterPath> getPathsFromRectangleCutThrough(QRectF rect, QLineF leftLineQList<QPainterPath> paths;paths << left << right;- qCritical() << "~~~ BOTH PATHS ^^^ ~~~";+ qCritical() << "~~~ BOTH PATHS ^^^ ~~~";return paths;}+QPointF findNearestPointOnLine(const QPointF &point, const QLineF &line, bool unbounded)+{+ if (line.length() == 0) {+ return point;+ }++ QPointF lineVector = line.p2() - line.p1();+ QPointF lineNormalVector = QPointF(-lineVector.y(), lineVector.x());++ QLineF pointToLineNormalLine = QLineF(point, point + lineNormalVector);+ QPointF result;+ QLineF::IntersectionType intersectionType = pointToLineNormalLine.intersects(line, &result);+ if (unbounded || intersectionType == QLineF::IntersectionType::BoundedIntersection) {+ return result;+ }+ qreal distance1 = kisDistance(line.p1(), result);+ qreal distance2 = kisDistance(line.p2(), result);+ if (distance1 + distance2 <= line.length() || qFuzzyCompare(distance1 + distance2, line.length())) {+ return result; // it's still on the line+ }+ if (distance1 < distance2) {+ return line.p1();+ }+ return line.p2();+}++QPointF movePointInTheDirection(const QPointF &point, const QPointF &direction, qreal distance)+{+ QPointF response = point;+ QLineF line = QLineF(QPointF(0, 0), direction);+ return response + distance*direction/line.length();+}++QPointF movePointAlongTheLine(const QPointF &point, const QLineF &direction, qreal distance, bool ensureOnLine)+{+ QPointF response = point;+ if (ensureOnLine) {+ response = findNearestPointOnLine(point, direction);+ }+ return movePointInTheDirection(response, direction.p2() - direction.p1(), distance);+}+}diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.hindex 321f1e8090..ed97b80c10 100644--- a/libs/global/kis_algebra_2d.h+++ b/libs/global/kis_algebra_2d.h@@ -859,6 +859,29 @@ QPointF KRITAGLOBAL_EXPORT moveElasticPoint(const QPointF &pt,const QVector<QPointF> &anchorPoints);+QPointF KRITAGLOBAL_EXPORT findNearestPointOnLine(const QPointF &point, const QLineF &line, bool unbounded = true);++/**+ * @brief movePointAlongTheLine moves the point a particular distance in the specified direction+ * @param point the point to move+ * @param direction the direction to move the point along+ * @param distance distance to move the point+ * @return the new position of the moved point+ */+QPointF KRITAGLOBAL_EXPORT movePointInTheDirection(const QPointF& point, const QPointF& direction, qreal distance);+++/**+ * @brief movePointAlongTheLine moves the point a particular distance in the specified direction+ * @param point the point to move+ * @param direction the direction to move the point along+ * @param distance distance to move the point+ * @param ensureOnLine if true, the algorithm will first change the point to the nearest point on the line, and then move it along the line+ * (if not true, it's an equivalent of movePointInTheDirection, with QPointF being a vector of direction)+ * @return the new position of the moved point+ */+QPointF KRITAGLOBAL_EXPORT movePointAlongTheLine(const QPointF& point, const QLineF& direction, qreal distance, bool ensureOnLine);+/*** @brief a simple class to generate Halton sequence*@@ -933,6 +956,7 @@ qreal KRITAGLOBAL_EXPORT findMinimumGoldenSection(std::function<qreal(qreal)> f,// NOTE: tiar: this function was debugged and should be working correctly but is not used anywhere any longerqreal KRITAGLOBAL_EXPORT findMinimumTernarySection(std::function<qreal(qreal)> f, qreal xA, qreal xB, qreal eps, int maxIter);+// kis_global has the same function, this needs to be removedqreal KRITAGLOBAL_EXPORT pointToLineDistSquared(const QPointF& pt, const QLineF& line);QList<QLineF> KRITAGLOBAL_EXPORT getParallelLines(const QLineF& line, const qreal distance);diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cppindex e216c46587..e2b6196918 100644--- a/libs/image/tests/kis_algebra_2d_test.cpp+++ b/libs/image/tests/kis_algebra_2d_test.cpp@@ -1087,6 +1087,25 @@ void KisAlgebra2DTest::testGetParallelLines()qCritical() << lines;qCritical() << "distances: " << sqrt(KisAlgebra2D::pointToLineDistSquared(QPointF(356.64,90.72), lines[0])) << sqrt(KisAlgebra2D::pointToLineDistSquared(QPointF(356.64,90.72), lines[1]));+ QPointF intersection;+ auto type = QLineF(QPointF(0, 0), QPointF(100, 0)).intersects(QLineF(QPointF(50, -50), QPointF(50, 100)), &intersection);+ qCritical() << "Intersecting lines: " << intersection << type;++}++void KisAlgebra2DTest::testGetParallelLines2()+{+ qreal gutterWidth = 20;++ QLineF gutterCenterLine = QLineF(QPointF(0, 0), QPointF(100, 1));++ QPointF gutterLineNormalVector = QPointF(-gutterCenterLine.dy(), gutterCenterLine.dx());+ QLineF line1 = QLineF(KisAlgebra2D::movePointInTheDirection(gutterCenterLine.p1(), gutterLineNormalVector, gutterWidth/2), KisAlgebra2D::movePointInTheDirection(gutterCenterLine.p2(), gutterLineNormalVector, gutterWidth/2));+ QLineF line2 = QLineF(KisAlgebra2D::movePointInTheDirection(gutterCenterLine.p1(), -gutterLineNormalVector, gutterWidth/2), KisAlgebra2D::movePointInTheDirection(gutterCenterLine.p2(), -gutterLineNormalVector, gutterWidth/2));++ qCritical() << line1 << line2;++}void KisAlgebra2DTest::testFindTrianglePoint()diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.hindex 83a2f17f23..35caff69fc 100644--- a/libs/image/tests/kis_algebra_2d_test.h+++ b/libs/image/tests/kis_algebra_2d_test.h@@ -38,6 +38,8 @@ private Q_SLOTS:void testCalculateConvexHull();void testGetPathsFromRectangleCutThrough();void testGetParallelLines();+ void testGetParallelLines2();+void testFindTrianglePoint();