Better calculation of angles

preferencesAboutTextFull
Craig Watson 8 years ago
parent 452383b8cc
commit a4b83e2c56
  1. 17
      src/frameworks/UBGeometryUtils.cpp
  2. 2
      src/frameworks/UBGeometryUtils.h

@ -255,8 +255,6 @@ QPolygonF UBGeometryUtils::arcToPolygon(const QLineF& startRadius, qreal spanAng
*/
QPolygonF UBGeometryUtils::curveToPolygon(const QList<QPointF>& points, qreal startWidth, qreal endWidth)
{
typedef QPair<QPointF, QPointF> pointPair;
int n_points = points.size();
if (n_points < 2)
@ -431,7 +429,18 @@ void UBGeometryUtils::crashPointList(QVector<QPointF> &points)
/**
* @brief Return the angle between three points
*/
qreal UBGeometryUtils::angle(const QPointF& a, const QPointF& b, const QPointF& c)
qreal UBGeometryUtils::angle(const QPointF& p1, const QPointF& p2, const QPointF& p3)
{
return (QLineF(a, b).angle() - QLineF(b, c).angle());
// 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
// opposite A, B and C, respectively
qreal a, b, c, beta;
a = qSqrt(qPow(p2.x() - p3.x(), 2) + qPow(p2.y() - p3.y(), 2));
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));
return 2*3.14159*beta;
}

@ -55,7 +55,7 @@ class UBGeometryUtils
static void crashPointList(QVector<QPointF> &points);
static qreal angle(const QPointF& a, const QPointF& b, const QPointF& c);
static qreal angle(const QPointF& p1, const QPointF& p2, const QPointF& p3);
const static int centimeterGraduationHeight;
const static int halfCentimeterGraduationHeight;

Loading…
Cancel
Save