From dd10e24083a1a2a6b5632ea37769972f4acb4686 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Sat, 4 Mar 2017 21:40:01 -0500 Subject: [PATCH] Fix angle calculation in geometry tools --- src/frameworks/UBGeometryUtils.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/frameworks/UBGeometryUtils.cpp b/src/frameworks/UBGeometryUtils.cpp index aa1ab020..80d5e448 100644 --- a/src/frameworks/UBGeometryUtils.cpp +++ b/src/frameworks/UBGeometryUtils.cpp @@ -427,12 +427,12 @@ void UBGeometryUtils::crashPointList(QVector &points) } /** - * @brief Return the angle between three points + * @brief Return the angle in degrees between three points */ qreal UBGeometryUtils::angle(const QPointF& p1, const QPointF& p2, const QPointF& p3) { // Angle between three points, using the law of cosines: - // The angle at B equals arccos((a^2-b^2-c^2)/(2*a*c)), where a, b and c are the sides of the triangle + // The angle at B equals arccos((a^2-b^2+c^2)/(2*a*c)), where a, b and c are the sides of the triangle // opposite A, B and C, respectively qreal a, b, c, beta; @@ -440,7 +440,10 @@ qreal UBGeometryUtils::angle(const QPointF& p1, const QPointF& p2, const QPointF b = qSqrt(qPow(p1.x() - p3.x(), 2) + qPow(p1.y() - p3.y(), 2)); c = qSqrt(qPow(p1.x() - p2.x(), 2) + qPow(p1.y() - p2.y(), 2)); - beta = qAcos((a*a - b*b - c*c)/(2*a*c)); + if (a == 0 || c == 0) + beta = 3.14159; + else + beta = qAcos((a*a - b*b + c*c)/(2*a*c)); - return 2*3.14159*beta; + return 180.* beta/3.14159; }