-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegments.cpp
More file actions
47 lines (45 loc) · 1.44 KB
/
segments.cpp
File metadata and controls
47 lines (45 loc) · 1.44 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
#include "segments.h"
bool rangeTest(int m, int c, double &tmin, double &tmax) {
if (m < 0) {
double r = 1. * c / m;
if (r > tmax) {
return false;
} else if (r > tmin) {
tmin = r;
}
} else if (m > 0) {
double r = 1. * c / m;
if (r < tmin) {
return false;
} else if (r < tmax) {
tmax = r;
}
} else if (c < 0) {
return false;
}
return true;
}
bool instersects(QLine &line, QRect &rect) {
double tmin = 0, tmax = 1;
int dx = line.x2() - line.x1();
int xmin = rect.x(), ymin = rect.y();
int xmax = xmin + rect.width(), ymax = ymin + rect.height();
// qInfo() << xmin << " " << ymin << " " << xmax << " " << ymax;
if (rangeTest(-dx, line.x1() - xmin, tmin, tmax)) {
if (rangeTest(dx, xmax - line.x1(), tmin, tmax)) {
int dy = line.y2() - line.y1();
if (rangeTest(-dy, line.y1() - ymin, tmin, tmax)) {
if (rangeTest(dy, ymax - line.y1(), tmin, tmax)) {
if (tmax < 1) {
line.setP2(QPoint((int)(line.x1() + tmax*dx), (int)(line.y1() + tmax*dy)));
}
if (tmin > 0) {
line.setP1(QPoint((int)(line.x1() + tmin*dx), (int)(line.y1() + tmin*dy)));
}
return true;
}
}
}
}
return false;
}