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
Definition
- Class:
- PointInPolygon
- Method:
- testPoint
- Parameters:
- String[], int, int
- Returns:
- String
- Method signature:
- String testPoint(String[] vertices, int testPointX, int testPointY)
- (be sure your method is public)
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
Examples
{"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"
{"0 0", "0 1000", "1000 1000","1000 800", "200 800","200 600","600 600", "600 400", "200 400","200 200","1000 200", "1000 0"}
300
300
Returns: "EXTERIOR"
{"0 0", "0 1000", "1000 1000","1000 800", "200 800","200 600","600 600", "600 400", "200 400","200 200","1000 200", "1000 0"}
0
500
Returns: "BOUNDARY"
{"0 1000", "1000 1000","1000 800", "200 800","200 600","600 600", "600 400", "200 400","200 200","1000 200", "1000 0", "0 0"}
0
500
Returns: "BOUNDARY"
{"0 1000","1000 1000","1000 800", "200 800","200 600","600 600","600 400", "200 400","200 200","1000 200","1000 0","0 0"}
322
333
Returns: "EXTERIOR"
{"0 1000", "1000 1000","1000 800", "200 800","200 600","600 600", "600 400", "200 400","200 200","1000 200", "1000 0", "0 0"}
555
999
Returns: "INTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
200
200
Returns: "INTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
400
200
Returns: "BOUNDARY"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
400
400
Returns: "EXTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
400
-400
Returns: "EXTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
-400
-400
Returns: "EXTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
-400
400
Returns: "EXTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
42
42
Returns: "INTERIOR"
{"500 0","500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0"}
499
1
Returns: "INTERIOR"
{"500 100","400 100","400 200","300 200", "300 300","200 300","200 400","100 400","100 500", "0 500","0 400","-100 400","-100 300","-200 300", "-200 200","-300 200","-300 100","-400 100","-400 0", "-500 0","-500 -100","-400 -100","-400 -200","-300 -200", "-300 -300","-200 -300","-200 -400","-100 -400","-100 -500", "0 -500","0 -400","100 -400","100 -300","200 -300", "200 -200","300 -200","300 -100","400 -100","400 0", "500 0"}
200
200
Returns: "INTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
1
-1
Returns: "INTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
1
1
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
-1
1
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
-1
-1
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
5
-10
Returns: "INTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
10
-15
Returns: "INTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
15
0
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
15
-5
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
10
5
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
-5
5
Returns: "EXTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
-15
15
Returns: "INTERIOR"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
0
-1
Returns: "BOUNDARY"
{"0 0","10 0","10 -10","20 -10","20 10", "-10 10","-10 -30","40 -30","40 30","-30 30", "-30 -50","60 -50","60 50","-50 50","-50 -70", "80 -70","80 70","-70 70","-70 -90","100 -90", "100 90","-90 90","-90 -110","120 -110","120 110", "-120 110","-120 120","130 120", "130 -120","-100 -120","-100 100","110 100","110 -100", "-80 -100","-80 80","90 80","90 -80","-60 -80", "-60 60","70 60","70 -60","-40 -60","-40 40", "50 40","50 -40","-20 -40","-20 20","30 20", "30 -20","0 -20"}
1
-20
Returns: "BOUNDARY"
{"1 0","2 0","2 1","3 1","3 0","4 0","4 -1","5 -1","5 0", "6 0","6 2","0 2","0 3","-1 3","-1 4","0 4","0 6","1 6", "1 7","0 7","0 8","-2 8","-2 2","-8 2","-8 0","-7 0", "-7 -1","-6 -1","-6 0","-4 0","-4 1","-3 1","-3 0", "-2 0","-2 -6","0 -6","0 -5","1 -5","1 -4","0 -4", "0 -3","-1 -3","-1 -2","0 -2","0 -1","1 -1"}
0
0
Returns: "INTERIOR"
{ "0 0", "0 10", "10 10", "10 0" }
5
10
Returns: "BOUNDARY"
{ "0 1000", "1000 1000", "1000 800", "200 800", "200 600", "600 600", "600 400", "200 400", "200 200", "1000 200", "1000 0", "0 0" }
322
333
Returns: "EXTERIOR"
{ "0 0", "0 10", "10 10", "10 0" }
10
15
Returns: "EXTERIOR"
{ "0 0", "3 0", "3 4", "-2 4", "-2 0", "-1 0", "-1 3", "2 3", "2 1", "0 1" }
1
2
Returns: "EXTERIOR"
{ "0 0", "0 10", "10 10", "10 0" }
10
10
Returns: "BOUNDARY"
{ "0 0", "0 1", "1 1", "1 0" }
0
10
Returns: "EXTERIOR"