-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerGeometry.cpp
More file actions
75 lines (69 loc) · 1.9 KB
/
IntegerGeometry.cpp
File metadata and controls
75 lines (69 loc) · 1.9 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
69
70
71
72
73
74
75
struct Point {
ll x, y;
bool operator==(const Point& rhs) const {
return x == rhs.x && y == rhs.y;
}
bool operator<(const Point& rhs) const {
return x == rhs.x ? y < rhs.y : x < rhs.x;
}
bool operator>(const Point& rhs) const {
return x == rhs.x ? y > rhs.y : x > rhs.x;
}
bool operator<=(const Point& rhs) const {
return x == rhs.x ? y <= rhs.y : x <= rhs.x;
}
bool operator>=(const Point& rhs) const {
return x == rhs.x ? y >= rhs.y : x >= rhs.x;
}
Point operator+(const Point& rhs) const {
return Point{ x + rhs.x, y + rhs.y };
}
Point operator-(const Point& rhs) const {
return Point{ x - rhs.x, y - rhs.y };
}
Point operator*(ll t) const {
return Point{ x * t, y * t };
}
};
int ccw(Point a, Point b, Point c) {
ll t = 1LL * a.x * (b.y - c.y) + 1LL * b.x * (c.y - a.y) + 1LL * c.x * (a.y - b.y);
return t ? (t > 0 ? 1 : -1) : 0;
}
struct Line {
Point a, b;
};
bool segment_intersect(Line p, Line q) {
Point a = p.a, b = p.b, c = q.a, d = q.b;
int ab = ccw(a, b, c) * ccw(a, b, d);
int cd = ccw(c, d, a) * ccw(c, d, b);
if (!ab && !cd) {
if (a > b) swap(a, b);
if (c > d) swap(c, d);
return a <= d && c <= b;
}
return ab <= 0 && cd <= 0;
}
vector<Point> convex_hull(vector<Point>& v) {
sort(v.begin(), v.end());
vector<Point> hi, lo;
for (Point& t : v) {
while (hi.size() >= 2 && ccw(*++hi.rbegin(), *hi.rbegin(), t) >= 0) hi.pop_back();
while (lo.size() >= 2 && ccw(*++lo.rbegin(), *lo.rbegin(), t) <= 0) lo.pop_back();
hi.push_back(t); lo.push_back(t);
}
hi.insert(hi.end(), ++lo.rbegin(), --lo.rend());
return hi;
}
const int INF = 1000000000;
bool point_in_polygon(vector<Point>& poly, Point k) {
Point temp = { k.x + 1, INF };
int s = 0;
for (int i = 0; i < poly.size(); i++)
if (segment_intersect({ poly[i], poly[(i + 1) % poly.size()] }, { k, temp })) s++;
return s % 2;
}
Point read_Point() {
ll t1, t2;
cin >> t1 >> t2; Point t;
return t = { t1,t2 };
}