This question already has an answer here:
Point in Polygon aka hit test
C# Point in polygon
Given a random polygon formulated with N line equations in the Cartesian coordinate system, is there any standard formula that is used to check for membership of a point (x,y)?
The simple solution is to get all the line formulas and check if point X is below this line, above that line and to the right of the other line, etc. But this will probably be tedious.
I should note that the polygon can be of any shape with any number of sides and may concave or convex.
For convenience I have already added these utility functions:
float slope(CGPoint p1, CGPoint p2)
{
return (p2.y - p1.y) / (p2.x - p1.x);
}
CGPoint pointOnLineWithY(CGPoint p, float m, float y)
{
float x = (y - p.y)/m + p.x;
return CGPointMake(x,y);
}
CGPoint pointOnLineWithX(CGPoint p, float m, float x)
{
float y = m*(x - p.x) + p.y;
return CGPointMake(x, y);
}