-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathPoint in Polygon.cpp
56 lines (47 loc) · 1.61 KB
/
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1001;
struct Point {
int x, y;
void read(){ scanf("%d %d", &x, &y); }
Point operator +(const Point& b) const { return Point{x+b.x, y+b.y}; }
Point operator -(const Point& b) const { return Point{x-b.x, y-b.y}; }
ll operator *(const Point& b) const { return (ll) x * b.y - (ll) y * b.x; }
void operator +=(const Point& b) { x += b.x; y += b.y; }
void operator -=(const Point& b) { x -= b.x; y -= b.y; }
void operator *=(const int k) { x *= k; y *= k; }
ll cross(const Point& b, const Point& c) const {
return (b - *this) * (c - *this);
}
};
int N, M;
Point P[maxN];
bool pointlineintersect(Point P1, Point P2, Point P3){
if(P2.cross(P1, P3) != 0) return false;
return (min(P2.x, P3.x) <= P1.x && P1.x <= max(P2.x, P3.x))
&& (min(P2.y, P3.y) <= P1.y && P1.y <= max(P2.y, P3.y));
}
void pointinpolygon(){
int cnt = 0;
bool boundary = false;
for(int i = 1; i <= N; i++){
int j = (i == N ? 1 : i+1);
if(pointlineintersect(P[0], P[i], P[j]))
boundary = true;
if(P[i].x <= P[0].x && P[0].x < P[j].x && P[0].cross(P[i], P[j]) < 0) cnt++;
else if(P[j].x <= P[0].x && P[0].x < P[i].x && P[0].cross(P[j], P[i]) < 0) cnt++;
}
if(boundary) printf("BOUNDARY\n");
else if(cnt&1) printf("INSIDE\n");
else printf("OUTSIDE\n");
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 1; i <= N; i++)
P[i].read();
for(int i = 0; i < M; i++){
P[0].read();
pointinpolygon();
}
}