-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
33 lines (26 loc) · 926 Bytes
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "utils.h"
#include <cmath>
double distance(const QPointF &p1, const QPointF &p2) {
auto delta = p1 - p2;
// return sqrt(delta.x() * delta.x() + delta.y() * delta.y());
return delta.manhattanLength();
}
double distance2(const QPointF &p1, const QPointF &p2) {
auto delta = p1 - p2;
return sqrt(delta.x() * delta.x() + delta.y() * delta.y());
}
double distance2_sq(const QPointF &p1, const QPointF &p2) {
auto delta = p1 - p2;
return delta.x() * delta.x() + delta.y() * delta.y();
}
double distance(const QPointF &point, const QLineF &line) {
auto x1 = line.x1();
auto y1 = line.y1();
auto x2 = line.x2();
auto y2 = line.y2();
auto u = ((point.x() - x1) * (x2 - x1) + (point.y() - y1) * (y2 - y1)) /
(line.length() * line.length());
auto x = x1 + u * (x2 - x1);
auto y = y1 + u * (y2 - y1);
return QLineF(point, QPointF(x, y)).length();
}