-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDistanceHelper.cpp
More file actions
68 lines (51 loc) · 1.63 KB
/
Copy pathCDistanceHelper.cpp
File metadata and controls
68 lines (51 loc) · 1.63 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <math.h>
#include "CDistanceHelper.h"
#include "commun.h"
double CDistanceHelper::calculDistance(CCircuit *circuit, QPointF p, QPointF oppose, QPointF &result, double angle) {
int x = p.x();
int y = p.y();
int dx, dy;
angle = CDistanceHelper::normAngle(angle);
if((angle > PI2 - 0.01 && angle < PI2 + 0.01) || (angle > 3 * PI2 - 0.01 && angle < 3 * PI2 + 0.01)) {
int sens = oppose.y() > p.y() ? -1 : 1;
bool fini = CDistanceHelper::isDehors(circuit, QPointF(x, y));
while(!fini) {
y += sens;
fini = CDistanceHelper::isDehors(circuit, QPointF(x, y));
}
} else {
double a = tan(angle);
double b = y - a * x;
int sens = oppose.x() > p.x() ? -1 : 1;
bool fini = CDistanceHelper::isDehors(circuit, QPointF(x, y));
while(!fini) {
x += sens;
y = a * x + b;
fini = CDistanceHelper::isDehors(circuit, QPointF(x, y));
}
}
circuit->normCoordonnees(x, y);
result = QPointF(x, y);
dx = abs(p.x() - x);
dy = abs(p.y() - y);
return sqrt(dx * dx + dy * dy);
}
bool CDistanceHelper::isDehors(CCircuit *circuit, const QPointF& p) {
QImage img = circuit->getImage();
QPoint pe;
if(p.x() < 0 || p.y() < 0 || p.x() >= img.width() || p.y() >= img.height()) {
return true;
}
pe = p.toPoint();
circuit->normCoordonnees(pe);
return img.pixel(pe) == 0xFF000000;
}
double CDistanceHelper::normAngle(double angle) {
while(angle < 0) {
angle += PI;
}
while(angle > 2 * PI) {
angle -= PI;
}
return angle;
}