PointInPolygon
SRM 187 · 2004-03-16 · by Rustyoldman
Problem Statement
Given a test point, (testPointX, testPointY), and the vertices of a simple polygon, vertices, determine if the test point is in the interior, in the exterior or on the boundary of the polygon. Return the
For simplicity, all sides of the polygon will be horizontal or vertical, and the vertices and the test
point will all be at integer coordinates. The x and y coordinates of the vertices will given in the
Notes
- A simple polygon is a polygon that may or may not be convex, but self-intersection is not allowed. Not even at a single point.
Constraints
- vertices will contain an even number of elements between 4 and 50 inclusive.
- Each element of vertices is formatted as follows "
" (quotes for clarity). With exactly one space between and and no leading or trailing spaces. and will consist of an optional minus sign followed by between 1 and 4 digit characters inclusive. There will be no leading zeros. - Each
and value in each element of vertices will be between -1000 and 1000 inclusive. - The elements of vertices taken in order will specify the vertices of a valid simple polygon.
- Each edge of this polygon will be either exactly horizontal or exactly vertical.
- No three consecutive vertices will be colinear.
- No two elements of vertices will be the same.
- No edges will overlap or intersect, except where adjacent edges meet pairwise at vertices.
- testPointX and testPointY will both be between -1000 and 1000 inclusive
{"0 0",
"0 10",
"10 10",
"10 0"}
5
5
Returns: "INTERIOR"
A simple example of a square of side 10.
{"0 0",
"0 10",
"10 10",
"10 0"}
10
15
Returns: "EXTERIOR"
Outside the same square.
{"0 0",
"0 10",
"10 10",
"10 0"}
5
10
Returns: "BOUNDARY"
On an edge of the square
{"-100 -90", "-100 100","100 100", "100 -100",
"-120 -100","-120 100","-130 100","-130 -110",
"110 -110", "110 110", "-110 110","-110 -90"}
0
0
Returns: "EXTERIOR"
A more complex geometry
{"0 0","0 1000","1000 1000","1000 800",
"200 800","200 600","600 600","600 400",
"200 400","200 200","1000 200","1000 0"}
100
500
Returns: "INTERIOR"
Submissions are judged against all 39 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class PointInPolygon with a public method string testPoint(vector<string> vertices, int testPointX, int testPointY) · 39 test cases · 2 s / 256 MB per case