Task: | Point in Polygon |
Sender: | Farah |
Submission time: | 2024-11-11 17:32:45 +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; typedef long long ll; // Structure to represent a point struct Point { ll x, y; }; // Function to check if a point is on a segment bool is_on_segment(const Point& p, const Point& a, const Point& b) { // Calculate the cross product ll cross = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x); if (cross != 0) return false; // Check if p is within the bounding rectangle of a and b ll minx = min(a.x, b.x); ll maxx = max(a.x, b.x); ll miny = min(a.y, b.y); ll maxy = max(a.y, b.y); return (minx <= p.x && p.x <= maxx) && (miny <= p.y && p.y <= maxy); } // Function to determine the position of a point relative to the polygon string point_in_polygon(const Point& p, const vector<Point>& polygon) { bool on_boundary = false; int crossings = 0; int n = polygon.size(); for(int i = 0; i < n; ++i){ Point a = polygon[i]; Point b = polygon[(i+1)%n]; // Check if the point is on the edge if(is_on_segment(p, a, b)){ on_boundary = true; break; } // Check if the ray crosses the edge // We consider the ray to the right, i.e., positive x direction // Check if the edge crosses the horizontal line at p.y // and if the intersection is to the right of p.x // Handle cases where the edge is horizontal if(a.y == b.y){ // Horizontal edge, ignore continue; } // Ensure a.y < b.y if(a.y > b.y){ swap(a, b); } // Check if p.y is within the range [a.y, b.y) if(p.y >= a.y && p.y < b.y){ // Compute the x coordinate of the intersection // To avoid floating point precision issues, use double double x_intersect = (double)(b.x - a.x) * (p.y - a.y) / (double)(b.y - a.y) + a.x; if(x_intersect > p.x){ crossings++; } } } if(on_boundary){ return "BOUNDARY"; } else{ return (crossings % 2 == 1) ? "INSIDE" : "OUTSIDE"; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<Point> polygon(n); for(int i = 0; i < n; ++i){ cin >> polygon[i].x >> polygon[i].y; } vector<Point> points(m); for(int i = 0; i < m; ++i){ cin >> points[i].x >> points[i].y; } for(int i = 0; i < m; ++i){ cout << point_in_polygon(points[i], polygon) << "\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 ... |
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 ... |
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 |