I need to write a function that will calculate if a point is inside polygon (true/false). Polygon always contains 4 points. I\'m reading polygons and points from SVG file
Given 4 points that define the vertices of a polygon, in order, clockwise or counterclockwise. To determine if a points is inside first workout if the polygon is concave by getting the cross product of each pair of lines.
The polygon is p1,p2,p3,p4 each have a x and y.
To get the cross product get 3 points, p1,p2,p3 where p2 is the vertex where the two lines join.
cross = (p2.x-p1.x) * (p3.y-p2.y) - (p2.y-p1.y) * (p3.x-p2.x);
Do for {p1,p2,p3}
, {p2,p3,p4}
,{p3,p4,p1}
, and {p4,p1,p2}
If the cross is the same sign for all verts (note 0 is both negative and positive) then the polygon is concave.
If not all the same only one can be different. You need to split the polygon into two 3 sided polygons by joining the different point with the one opposite.
Eg is cross of p2,p3,p4 is negative and the rest positive then you have two polygons p1,p2,p3 and p3,p4,p1
Now you have either one or two polygons.
Calculate the cross for the point you are testing against each line
cross = (p2.x-p1.x) * (testPoint.y-p1.y) - (p2.y-p1.y) * (testPoint.x-p1.x);
Do for each line {p1,p2}
, {p2,p3}
, {p3,p4}
,{p4,p1}
If the sign is the same for all the line segments then the point is inside the polygon. If you have two polygons, you only need to satisfy the same sign condition for one polygon