Task: | Point in Polygon |
Sender: | bubu2006 |
Submission time: | 2024-11-11 16:24:43 +0200 |
Language: | C++ (C++20) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.01 s | details |
#2 | ACCEPTED | 0.02 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | ACCEPTED | 0.00 s | details |
#10 | ACCEPTED | 0.00 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.00 s | details |
#13 | ACCEPTED | 0.00 s | details |
Code
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; Point () { x = y = 0; } Point (double xx, double yy) { x = xx, y = yy; } void read() { cin >> x >> y; } }; Point operator - (const Point& a, const Point& b) { return Point(a.x - b.x, a.y - b.y); } Point operator + (const Point& a, const Point& b) { return Point(a.x + b.x, a.y + b.y); } double cross(const Point& a, const Point& b) { return a.x * b.y - b.x * a.y; } // -1 -> ccw int ccw(const Point& a, const Point& b, const Point& c) { Point ab = b - a; Point ac = c - a; int t = cross(ab, ac); if (t > 0) return -1; else if (t < 0) return 1; else return 0; } bool onseg(const Point& a, const Point& b, const Point& c) { return min(b.x, c.x) <= a.x && a.x <= max(b.x, c.x) && min(b.y, c.y) <= a.y && a.y <= max(b.y, c.y); } int inpoly(const vector<Point>& p, const Point& q) { int n = p.size(); // check if point on edge for (int i = 0; i < n; i++) { Point u = p[i]; Point v = p[(i + 1) % n]; if (ccw(u, v, q) == 0 && onseg(q, u, v)) return 0; } // check if point inside bool ok = 0; for (int i = 0; i < n; i++) { Point u = p[i]; Point v = p[(i + 1) % n]; if (((u.y <= q.y && q.y < v.y) || (v.y <= q.y && q.y < u.y)) && (q.x < u.x + (v.x - u.x) * (q.y - u.y) / (v.y - u.y))) ok = !ok; } if (ok) return 1; else return -1; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<Point> p(n); for (int i = 0; i < n; i++) p[i].read(); while (m--) { Point q; q.read(); int ans = inpoly(p, q); if (ans == 1) cout << "INSIDE\n"; else if (ans == -1) cout << "OUTSIDE\n"; else cout << "BOUNDARY\n"; } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
100 1000 -7 -19 91 77 100 100 64 60 ... |
correct output |
---|
INSIDE OUTSIDE INSIDE INSIDE INSIDE ... |
user output |
---|
INSIDE OUTSIDE INSIDE INSIDE INSIDE ... Truncated |
Test 2
Verdict: ACCEPTED
input |
---|
1000 1000 365625896 -113418831 278762563 38777445 250367343 -96991975 175866909 -129766978 ... |
correct output |
---|
OUTSIDE OUTSIDE INSIDE OUTSIDE OUTSIDE ... |
user output |
---|
OUTSIDE OUTSIDE INSIDE OUTSIDE OUTSIDE ... Truncated |
Test 3
Verdict: ACCEPTED
input |
---|
4 1 1 5 5 5 5 1 1 1 ... |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |
Test 4
Verdict: ACCEPTED
input |
---|
4 1 1 5 5 5 5 1 1 1 ... |
correct output |
---|
OUTSIDE |
user output |
---|
OUTSIDE |
Test 5
Verdict: ACCEPTED
input |
---|
4 1 1 100 2 50 1 20 0 50 ... |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |
Test 6
Verdict: ACCEPTED
input |
---|
8 1 0 0 0 2 1 1 2 2 ... |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |
Test 7
Verdict: ACCEPTED
input |
---|
4 4 0 0 3 0 3 4 0 4 ... |
correct output |
---|
INSIDE BOUNDARY OUTSIDE BOUNDARY |
user output |
---|
INSIDE BOUNDARY OUTSIDE BOUNDARY |
Test 8
Verdict: ACCEPTED
input |
---|
6 1 0 0 0 2 3 1 2 2 ... |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |
Test 9
Verdict: ACCEPTED
input |
---|
3 1 0 0 1 1000000000 -3 0 1 1 |
correct output |
---|
OUTSIDE |
user output |
---|
OUTSIDE |
Test 10
Verdict: ACCEPTED
input |
---|
3 1 -100000 0 -1000000000 -999999999 1000000000 1000000000 0 0 |
correct output |
---|
OUTSIDE |
user output |
---|
OUTSIDE |
Test 11
Verdict: ACCEPTED
input |
---|
3 1 -100000 0 -999999999 -1000000000 1000 1000 0 0 |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |
Test 12
Verdict: ACCEPTED
input |
---|
4 1 -4 1 -6 1 -6 -1 -4 -1 ... |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |
Test 13
Verdict: ACCEPTED
input |
---|
3 1 0 10 0 -10 10 0 1 0 |
correct output |
---|
INSIDE |
user output |
---|
INSIDE |