Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Fix calculating parallel lines

Agata Cacko0c79059744bb
diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cpp
index 79b1a796f1..86a6c37082 100644
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@ -1564,33 +1564,51 @@ QList<QLineF> getParallelLines(const QLineF& line, const qreal distance) {
if (line.length() == 0) {
return QList<QLineF>();
}
+ if (qFuzzyCompare(distance, 0)) {
+ return QList<QLineF>() << line << line;
+ }
QPointF lineVector = line.p2() - line.p1();
+ //lineVector /= line.length();
// dist(P, l) = |Ax + By + C|/(sqrt(A^2 + B^2))
- // dist(P, l) = |Vy*x - Vx*y + (p2x*p1y - p2y*p1x)|/(sqrt(line.length()))
+ // dist(P, l) = |Vy*x - Vx*y + (p2x*p1y - p2y*p1x)|/(line.length())
// |Vy*x - Vx*y + M| = distance*sqrt(line.length())
// // that gives us two line equations:
// Vy*x - Vx*y + M +/- distance*sqrt(line.length()) = 0
+ // -> Vy*x = Vx*y - M +/- distance*sqrt(line.length()) = 0 => x = (Vx*y - N)/Vy
+ // -> Vx*y = Vy*x + M +/- distance*sqrt(line.length()) = 0 => y = (Vy*x + N)/Vx
+
// if Vy != 0:
- // x = (Vx*y + N)/Vy // where N = M +/- distance*sqrt(line.length())
+ // x = (Vx*y + N)/Vy // where N = M +/- distance*line.length()
// else:
- // y = (Vy*x + N)/Vx // where N = M +/- distance*sqrt(line.length())
+ // y = (Vy*x + N)/Vx // where N = M +/- distance*line.length()
qreal M = line.p2().x()*line.p1().y() - line.p2().y()*line.p1().x();
- qreal N1 = M - distance*sqrt(line.length());
- qreal N2 = M + distance*sqrt(line.length());
+ qreal N1 = M - distance*line.length();
+ qreal N2 = M + distance*line.length();
qreal Vx = lineVector.x();
qreal Vy = lineVector.y();
+ qCritical() << "Question was: " << ppVar(line) << ppVar(distance);
+ qCritical() << "Values: " << ppVar(lineVector) << ppVar(M) << ppVar(N1) << ppVar(N2) << ppVar(Vx) << ppVar(Vy);
+ qCritical() << "So in theory: dist(P, l) = |" << Vy << "*x - " << Vx << "*y + " << M << "|/line.length()";
+ qCritical() << "So in theory: dist(P, l)*line.length() = +/-" << Vy << "*x - " << Vx << "*y + " << M << "";
+
auto calculateSecondDimension = [](qreal dim1, qreal N, qreal V1, qreal V2) {
//(Vx*y + N)/Vy
+ //(Vy*x + N)/Vx
+ //(V2*dim1 + N)/V1
+ qCritical() << "Calculate second dimension, values: " << ppVar(dim1) << ppVar(N) << ppVar(V1) << ppVar(V2);
+ qCritical() << "Calculate second dimension, equation: (" << dim1 << "*" << V2 << " + " << N << ")/" << V1 << " = " << (dim1*V2 + N)/V1;
+
return (dim1*V2 + N)/V1;
};
QList<QLineF> responses;
if (lineVector.x() != 0) {
+ qCritical() << "Case 1.";
// y = (Vx*y + N)/Vx // where N = M +/- distance*sqrt(line.length())
qreal x1 = line.p1().x();
@@ -1599,15 +1617,20 @@ QList<QLineF> getParallelLines(const QLineF& line, const qreal distance) {
responses << QLineF(x1, calculateSecondDimension(x1, N1, Vx, Vy), x2, calculateSecondDimension(x2, N1, Vx, Vy));
responses << QLineF(x1, calculateSecondDimension(x1, N2, Vx, Vy), x2, calculateSecondDimension(x2, N2, Vx, Vy));
- } else {
+ } else { // Vx == 0
+ qCritical() << "Case 2.";
qreal y1 = line.p1().y();
qreal y2 = line.p2().y();
- //qreal y1 = (lineVector.y()*x1 + N1
- responses << QLineF(calculateSecondDimension(y1, N1, Vy, Vx), y1, calculateSecondDimension(y2, N1, Vy, Vx), y2);
- responses << QLineF(calculateSecondDimension(y1, N2, Vy, Vx), y1, calculateSecondDimension(y2, N2, Vy, Vx), y2);
+ //qreal y1 = (lineVector.y()*x1 - N1
+ responses << QLineF(calculateSecondDimension(y1, -N1, Vy, Vx), y1, calculateSecondDimension(y2, -N1, Vy, Vx), y2);
+ responses << QLineF(calculateSecondDimension(y1, -N2, Vy, Vx), y1, calculateSecondDimension(y2, -N2, Vy, Vx), y2);
}
+ qCritical() << "Response 1: " << responses[0];
+ qCritical() << "Response 2: " << responses[1];
+
+
return responses;
}
diff --git a/libs/image/tests/kis_algebra_2d_test.cpp b/libs/image/tests/kis_algebra_2d_test.cpp
index c474244423..e216c46587 100644
--- a/libs/image/tests/kis_algebra_2d_test.cpp
+++ b/libs/image/tests/kis_algebra_2d_test.cpp
@@ -1060,6 +1060,32 @@ void KisAlgebra2DTest::testGetPathsFromRectangleCutThrough()
QList<QPainterPath> paths = KisAlgebra2D::getPathsFromRectangleCutThrough(rect, left, right);
qCritical() << paths;
+ assert(paths.length() == 2 && "Must be two paths returned");
+ assert(paths[0].elementCount() == 4 && "First path must have 4 elements (it's a closed triangle)");
+ assert(paths[1].elementCount() == 6 && "First path must have 6 elements");
+
+
+}
+
+void KisAlgebra2DTest::testGetParallelLines()
+{
+ QList<QLineF> lines = KisAlgebra2D::getParallelLines(QLineF(QPointF(100, 200), QPointF(200, 200)), 100);
+ qCritical() << lines;
+ lines = KisAlgebra2D::getParallelLines(QLineF(QPointF(100, 200), QPointF(500, 200)), 100);
+ qCritical() << lines;
+
+ lines = KisAlgebra2D::getParallelLines(QLineF(QPointF(100, 200), QPointF(100, 400)), 100);
+ qCritical() << lines;
+
+ lines = KisAlgebra2D::getParallelLines(QLineF(QPointF(347.16,190.8),QPointF(366.72,190.8)), 50);
+ qCritical() << lines;
+
+
+ // QLineF(QPointF(356.64,90.72),QPointF(356.64,104.64)
+
+ lines = KisAlgebra2D::getParallelLines(QLineF(QPointF(356.64,90.72),QPointF(356.64,104.64)), 50);
+ qCritical() << lines;
+ qCritical() << "distances: " << sqrt(KisAlgebra2D::pointToLineDistSquared(QPointF(356.64,90.72), lines[0])) << sqrt(KisAlgebra2D::pointToLineDistSquared(QPointF(356.64,90.72), lines[1]));
}
diff --git a/libs/image/tests/kis_algebra_2d_test.h b/libs/image/tests/kis_algebra_2d_test.h
index b121a2404e..83a2f17f23 100644
--- a/libs/image/tests/kis_algebra_2d_test.h
+++ b/libs/image/tests/kis_algebra_2d_test.h
@@ -37,6 +37,7 @@ private Q_SLOTS:
void testCalculateConvexHull();
void testGetPathsFromRectangleCutThrough();
+ void testGetParallelLines();
void testFindTrianglePoint();