From 9794cc6042e95e8685008c229ac67ebc9415827b Mon Sep 17 00:00:00 2001 From: Isar Date: Fri, 19 Mar 2021 19:24:32 +0100 Subject: [PATCH 1/4] Fix measure tool glitch at 180 degrees --- src/ui/tools/measure-tool.cpp | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/ui/tools/measure-tool.cpp b/src/ui/tools/measure-tool.cpp index 9891f1bb4e..b7fa3979a7 100644 --- a/src/ui/tools/measure-tool.cpp +++ b/src/ui/tools/measure-tool.cpp @@ -293,7 +293,41 @@ void createAngleDisplayCurve(SPDesktop *desktop, double q1 = (ax * ax) + (ay * ay); double q2 = q1 + (ax * bx) + (ay * by); - double k2 = (4.0 / 3.0) * (std::sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx)); + double k2; + + /* + * The denominator of the expression for k2 can become 0, so this should be handled. + * The function for k2 tends to a limit for very small values of (ax * by) - (ay * bx), so theoretically + * it should be correct for values close to 0, however due to floating point inaccuracies this + * is not the case, and instabilities still exist. Therefore do a range check on the denominator. + * (This also solves some instances where again due to floating point inaccuracies, the square root term + * becomes slightly negative in case of very small values for ax * by - ay * bx). + * The values of this range have been generated by trying to make this term as small as possible, + * by zooming in as much as possible in the GUI, using the measurement tool and + * trying to get as close to 180 or 0 degrees as possible. + * Smallest value I was able to get was around 1e-5, and then I added some zeroes for good measure. + */ + if( !((ax * by - ay * bx < 0.00000000001) && (ax * by - ay * bx > -0.00000000001)) ){ + k2 = (4.0 / 3.0) * (std::sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx)); + } + else{ + // If the numerator is 0, there are 2 cases: + // Either the angle is (almost) +-180 degrees, in which case the limit of k2 tends to -+4.0/3.0. + if(angle > 3.14 || angle < -3.14){ // The angle is in radians + // Now there are also 2 cases, where inkscape thinks it is 180 degrees, or -180 degrees. + // Adjust the value of k2 accordingly + if(angle > 0){ + k2 = -4.0/3.0; + } + else{ + k2 = 4.0/3.0; + } + } + // if the angle is (almost) 0, k2 is equal to 0 + else{ + k2 = 0.0; + } + } Geom::Point p2(xc + ax - (k2 * ay), yc + ay + (k2 * ax)); -- GitLab From 06b441b02ca4dc99f189fd799f3c12e1ca61e4e6 Mon Sep 17 00:00:00 2001 From: Isar Date: Sun, 21 Mar 2021 13:07:17 +0100 Subject: [PATCH 2/4] Fix typo in comment --- src/ui/tools/measure-tool.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/tools/measure-tool.cpp b/src/ui/tools/measure-tool.cpp index b7fa3979a7..415fb07c9f 100644 --- a/src/ui/tools/measure-tool.cpp +++ b/src/ui/tools/measure-tool.cpp @@ -311,7 +311,7 @@ void createAngleDisplayCurve(SPDesktop *desktop, k2 = (4.0 / 3.0) * (std::sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx)); } else{ - // If the numerator is 0, there are 2 cases: + // If the denominator is 0, there are 2 cases: // Either the angle is (almost) +-180 degrees, in which case the limit of k2 tends to -+4.0/3.0. if(angle > 3.14 || angle < -3.14){ // The angle is in radians // Now there are also 2 cases, where inkscape thinks it is 180 degrees, or -180 degrees. -- GitLab From 6743d684399ed156e267898899754fba825b7a88 Mon Sep 17 00:00:00 2001 From: Isar Date: Sun, 21 Mar 2021 13:27:07 +0100 Subject: [PATCH 3/4] Fix formatting --- src/ui/tools/measure-tool.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/ui/tools/measure-tool.cpp b/src/ui/tools/measure-tool.cpp index 415fb07c9f..e4dc61fb23 100644 --- a/src/ui/tools/measure-tool.cpp +++ b/src/ui/tools/measure-tool.cpp @@ -307,24 +307,21 @@ void createAngleDisplayCurve(SPDesktop *desktop, * trying to get as close to 180 or 0 degrees as possible. * Smallest value I was able to get was around 1e-5, and then I added some zeroes for good measure. */ - if( !((ax * by - ay * bx < 0.00000000001) && (ax * by - ay * bx > -0.00000000001)) ){ + if ( !((ax * by - ay * bx < 0.00000000001) && (ax * by - ay * bx > -0.00000000001)) ) { k2 = (4.0 / 3.0) * (std::sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx)); - } - else{ + } else { // If the denominator is 0, there are 2 cases: // Either the angle is (almost) +-180 degrees, in which case the limit of k2 tends to -+4.0/3.0. - if(angle > 3.14 || angle < -3.14){ // The angle is in radians + if (angle > 3.14 || angle < -3.14) { // The angle is in radians // Now there are also 2 cases, where inkscape thinks it is 180 degrees, or -180 degrees. // Adjust the value of k2 accordingly - if(angle > 0){ + if (angle > 0) { k2 = -4.0/3.0; - } - else{ + } else { k2 = 4.0/3.0; } - } + } else { // if the angle is (almost) 0, k2 is equal to 0 - else{ k2 = 0.0; } } -- GitLab From 52fb9d438918b360437ffb58be8c14379070e596 Mon Sep 17 00:00:00 2001 From: Isar Date: Sun, 21 Mar 2021 13:30:38 +0100 Subject: [PATCH 4/4] Fix formatting --- src/ui/tools/measure-tool.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ui/tools/measure-tool.cpp b/src/ui/tools/measure-tool.cpp index e4dc61fb23..cf2bfb4f81 100644 --- a/src/ui/tools/measure-tool.cpp +++ b/src/ui/tools/measure-tool.cpp @@ -306,8 +306,8 @@ void createAngleDisplayCurve(SPDesktop *desktop, * by zooming in as much as possible in the GUI, using the measurement tool and * trying to get as close to 180 or 0 degrees as possible. * Smallest value I was able to get was around 1e-5, and then I added some zeroes for good measure. - */ - if ( !((ax * by - ay * bx < 0.00000000001) && (ax * by - ay * bx > -0.00000000001)) ) { + */ + if (!((ax * by - ay * bx < 0.00000000001) && (ax * by - ay * bx > -0.00000000001))) { k2 = (4.0 / 3.0) * (std::sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx)); } else { // If the denominator is 0, there are 2 cases: @@ -316,9 +316,9 @@ void createAngleDisplayCurve(SPDesktop *desktop, // Now there are also 2 cases, where inkscape thinks it is 180 degrees, or -180 degrees. // Adjust the value of k2 accordingly if (angle > 0) { - k2 = -4.0/3.0; + k2 = -4.0 / 3.0; } else { - k2 = 4.0/3.0; + k2 = 4.0 / 3.0; } } else { // if the angle is (almost) 0, k2 is equal to 0 -- GitLab