* @brief Return the angle in degrees between three points */
| 421 | * @brief Return the angle in degrees between three points |
| 422 | */ |
| 423 | qreal UBGeometryUtils::angle(const QPointF& p1, const QPointF& p2, const QPointF& p3) |
| 424 | { |
| 425 | // Angle between three points, using the law of cosines: |
| 426 | // 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 |
| 427 | // opposite A, B and C, respectively |
| 428 | |
| 429 | qreal a, b, c, beta; |
| 430 | a = qSqrt(qPow(p2.x() - p3.x(), 2) + qPow(p2.y() - p3.y(), 2)); |
| 431 | b = qSqrt(qPow(p1.x() - p3.x(), 2) + qPow(p1.y() - p3.y(), 2)); |
| 432 | c = qSqrt(qPow(p1.x() - p2.x(), 2) + qPow(p1.y() - p2.y(), 2)); |
| 433 | |
| 434 | if (a == 0 || c == 0) |
| 435 | beta = 3.14159; |
| 436 | else |
| 437 | beta = qAcos(std::max(-1.0, std::min(1.0, (a*a - b*b + c*c)/(2*a*c)))); |
| 438 | |
| 439 | |
| 440 | return 180.* beta/3.14159; |
| 441 | } |
| 442 | |
| 443 | |
| 444 |
no outgoing calls
no test coverage detected