Once I tested the algorithm explained here and here, I was as excited as the comments below it.
But after my test cases failed then a lot of debugging and tracing, that I realized it has a condition for it to work properly.
This algorithm needs the polygon List<Point>
to have its' points sorted counterclockwise, otherwise the output is incorrect.
This is a very simple test case to validate my claim :
(You can use this to plot and test)
Polygon: Points sorted counterclockwise.
inputPoints.Add(new Point(1, 3));
inputPoints.Add(new Point(2, 1));
inputPoints.Add(new Point(6, 2));
inputPoints.Add(new Point(3, 6));
Test points:
Point testP1 = new Point(3, 3); //inside, algorithm output :correct
Point testP2 = new Point(6, 3); //outside, algorithm output :correct
Point testP3 = new Point(9, 9); //outside, algorithm output :correct
Polygon: Points sorted clockwise.
inputPoints.Add(new Point(1, 3));
inputPoints.Add(new Point(2, 1));
inputPoints.Add(new Point(6, 2));
inputPoints.Add(new Point(3, 6));
Test points:
Point testP1 = new Point(3, 3); //inside, algorithm output :correct
Point testP2 = new Point(6, 3); //outside, algorithm output :wrong answer
Point testP3 = new Point(9, 9); //outside, algorithm output :correct
Polygon: Points in random order (they're only 4 points, but when scaled, will result incorrect as well).
inputPoints.Add(new Point(2, 1));
inputPoints.Add(new Point(6, 2));
inputPoints.Add(new Point(1, 3));
inputPoints.Add(new Point(3, 6));
Test points:
Point testP1 = new Point(3, 3); //inside, algorithm output :wrong answer
Point testP2 = new Point(6, 3); //outside, algorithm output :correct
Point testP3 = new Point(9, 9); //outside, algorithm output :correct