-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_LineIntersection.cpp
More file actions
65 lines (58 loc) · 2.45 KB
/
9_LineIntersection.cpp
File metadata and controls
65 lines (58 loc) · 2.45 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
#include <bits/stdc++.h>
#define lineType pair<pair<int, int>, pair<int, int>>
#define pointType pair<int, int>
using namespace std;
class LineIntersection {
lineType linePQ, lineRS;
bool isOnSegment(lineType lineSegment, pointType pointToCheck) {
if (pointToCheck.first >=
min(lineSegment.first.first, lineSegment.second.first) and
pointToCheck.first <=
max(lineSegment.first.first, lineSegment.second.first))
if (pointToCheck.second >=
min(lineSegment.first.second, lineSegment.second.second) and
pointToCheck.second <=
max(lineSegment.first.second, lineSegment.second.second))
return true;
return false;
}
int findDirection(lineType lineSegment, pointType pointToCheck) {
int directionValue =
((pointToCheck.first - lineSegment.first.first) *
(lineSegment.second.second - lineSegment.first.second));
directionValue -= ((pointToCheck.second - lineSegment.first.second) *
(lineSegment.second.first - lineSegment.first.second));
return directionValue / abs(directionValue);
}
public:
LineIntersection(lineType linePQ, lineType lineRS)
: linePQ(linePQ), lineRS(lineRS) {}
bool isIntersecting() {
int directionPQR = findDirection(linePQ, lineRS.first);
int directionPQS = findDirection(linePQ, lineRS.second);
int directionRSP = findDirection(lineRS, linePQ.first);
int directionRSQ = findDirection(lineRS, linePQ.second);
if (directionPQR * directionPQS < 0 and directionRSP * directionRSQ < 0)
return true;
if (directionPQR == 0 and isOnSegment(linePQ, lineRS.first)) return true;
if (directionPQS == 0 and isOnSegment(linePQ, lineRS.second)) return true;
if (directionRSP == 0 and isOnSegment(lineRS, linePQ.first)) return true;
if (directionRSQ == 0 and isOnSegment(lineRS, linePQ.second)) return true;
return false;
}
};
int main() {
int x1, y1, x2, y2, x3, y3, x4, y4;
cout << "Enter the coordinates of the first line segment: ";
cin >> x1 >> y1 >> x2 >> y2;
cout << "Enter the coordinates of the second line segment: ";
cin >> x3 >> y3 >> x4 >> y4;
LineIntersection lineIntersection(
make_pair(make_pair(x1, y1), make_pair(x2, y2)),
make_pair(make_pair(x3, y3), make_pair(x4, y4)));
if (lineIntersection.isIntersecting())
cout << "The two line segments intersect." << endl;
else
cout << "The two line segments do not intersect." << endl;
return 0;
}