I have tried two algorithms that were answers in other StackOverflow questions:
Both were showing some points in as out or out as in while other points were correct. This test cast assumes that there are always only 4 vertices (rectangle)
bool PointInPolygon(Coordinate point, vector<Coordinate> points) {
cout << "x,y" << point.getX() << "," << point.getY() << endl;
cout << "TEST COOR ARRAY" << endl;
for (int i=0; i<4; i++) {
cout << points[i].getX() << "," << points[i].getY() << endl;
}
int i, j, nvert = points.size();
bool c = false;
for(i = 0, j = nvert - 1; i < nvert; j = i++) {
if( ( (points[i].getY() > point.getY() ) != (points[j].getY() > point.getY()) ) &&
(point.getX() < (points[j].getX() - points[i].getX()) * (point.getY() - points[i].getY()) / (points[j].getY() - points[i].getY()) + points[i].getX())
)
c = !c;
}
cout << c << "======================" << endl;
return c;
}
And the output was wrong where (2,3) and (1,1) shouldn't be in. Lines on the perimeter are not considered to be in. But even so, then 2,3 should always be in.
x,y1,1
TEST COOR ARRAY
1,1
1,3
4,3
4,1
1======================
IN
x,y2,2
TEST COOR ARRAY
1,1
1,3
4,3
4,1
1======================
IN
x,y2,3
TEST COOR ARRAY
1,1
1,3
4,3
4,1
0======================
OUT
x,y3,2
TEST COOR ARRAY
1,1
1,3
4,3
4,1
1======================
IN
x,y3,3
TEST COOR ARRAY
1,1
1,3
4,3
4,1
0======================
OUT
I have similar issues in using the other algorithms I found as well. If anyone could point me in the right direction, I'd appreciate it a lot thanks!