Skip to content

Commit 1cda410

Browse files
committed
Reverted line intersection
1 parent 58edcdd commit 1cda410

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Intersection of line segments
2+
## \note Line segments are given by the coordinates of their end points.
3+
struct Point {
4+
5+
long long x, y;
6+
7+
};
8+
9+
struct Line {
10+
Point p, q;
11+
};
12+
13+
int sgn(long long x) {
14+
return x == 0 ? 0 : (x < 0 ? -1 : 1);
15+
}
16+
17+
long long det(long long x1, long long y1, long long x2, long long y2) {
18+
return x1 * y2 - x2 * y1;
19+
}
20+
21+
int orientation(Point p, Point q, Point r) {
22+
return sgn(det(p.x - r.x, p.y - r.y, q.x - r.x, q.y - r.y));
23+
}
24+
25+
bool intersect(Line l1, Line l2) {
26+
if (max(l1.p.x, l1.q.x) < min(l2.p.x, l2.q.x)) return false;
27+
if (max(l2.p.x, l2.q.x) < min(l1.p.x, l1.q.x)) return false;
28+
if (max(l1.p.y, l1.q.y) < min(l2.p.y, l2.q.y)) return false;
29+
if (max(l2.p.y, l2.q.y) < min(l1.p.y, l1.q.y)) return false;
30+
return orientation(l1.q, l2.p, l1.p) * orientation(l1.q, l2.q, l1.p) <= 0
31+
&& orientation(l2.q, l1.p, l2.p) * orientation(l2.q, l1.q, l2.p) <= 0;
32+
}

main.pdf

2.94 KB
Binary file not shown.

0 commit comments

Comments
 (0)