-
Notifications
You must be signed in to change notification settings - Fork 156
/
2192 - Point in Polygon.cpp
78 lines (69 loc) · 1.87 KB
/
2192 - Point in Polygon.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
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
76
77
78
/*
Problem Name: Point in Polygon
Problem Link: https://cses.fi/problemset/task/2192
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define P complex<int>
#define X real()
#define Y imag()
const int INF = 1e9 + 7;
int cross(P a, P b, P c) {
P u = b - a;
P v = c - a;
int cp = (conj(u)*v).Y;
return (cp > 0) - (cp < 0);
}
bool comp(const P &a, const P &b) {
return (a.X == b.X) ? (a.Y < b.Y) : (a.X < b.X);
}
bool mid(P a, P b, P c) {
vector<P> v = {a, b, c};
sort(v.begin(), v.end(), comp);
return (v[1] == c);
}
bool check(P a, P b, P c, P d) {
int cp1 = cross(a, b, c);
int cp2 = cross(a, b, d);
int cp3 = cross(c, d, a);
int cp4 = cross(c, d, b);
if (cp1 * cp2 < 0 && cp3 * cp4 < 0) return 1;
if (cp3 == 0 && mid(c, d, a) && cp4 < 0) return 1;
if (cp4 == 0 && mid(c, d, b) && cp3 < 0) return 1;
return 0;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
//https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
int n, m; cin >> n >> m;
vector<P> v;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
v.push_back({x, y});
}
v.push_back(v[0]);
while (m--) {
int x, y; cin >> x >> y;
P a = {x, y};
P b = {INF, INF};
int cnt = 0;
int flag = 0;
for (int i = 0; i < n; i++) {
int cp = cross(v[i], v[i+1], a);
if (cp == 0 && mid(v[i], v[i+1], a)) {
flag = 1;
break;
}
cnt += check(v[i], v[i+1], a, b);
}
if (flag) cout << "BOUNDARY" << endl;
else cout << (cnt & 1 ? "INSIDE" : "OUTSIDE") << endl;
}
}