-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeometry_integer.cpp
47 lines (40 loc) · 1.09 KB
/
geometry_integer.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// not verified!!
// hoge
#include <bits/stdc++.h>
using namespace std;
using i128 = __int128_t;
struct vec2 {
i128 x, y;
vec2 operator-(vec2 rhs) {
return {x - rhs.x, y - rhs.y};
}
bool operator==(vec2 rhs) {
return x == rhs.x && y == rhs.y;
}
};
struct seg {
vec2 a, b;
};
i128 cross(vec2 a, vec2 b) {
return a.x * b.y - a.y * b.x;
}
bool is_crossed(seg s, seg t) {
vec2 p1 = s.a, p2 = s.b, p3 = t.a, p4 = t.b;
i128 t1 = (p1.x - p2.x) * (p3.y - p1.y) + (p1.y - p2.y) * (p1.x - p3.x);
i128 t2 = (p1.x - p2.x) * (p4.y - p1.y) + (p1.y - p2.y) * (p1.x - p4.x);
i128 t3 = (p3.x - p4.x) * (p1.y - p3.y) + (p3.y - p4.y) * (p3.x - p1.x);
i128 t4 = (p3.x - p4.x) * (p2.y - p3.y) + (p3.y - p4.y) * (p3.x - p2.x);
return t1 * t2 <= 0 && t3 * t4 <= 0;
// return t1 * t2 < 0 && t3 * t4 < 0;
}
bool is_parallel(seg s, seg t) {
return !cross(s.b - s.a, t.b - t.a);
}
i128 signedArea2(vector<vec2> ps) {
int n = ps.size();
i128 sum = 0;
for (int i = 0; i < n; i++) {
sum += cross(ps[i], ps[(i + 1) % n]);
}
return sum;
}