There is an easy way to check if a point is in a polygon. According to this Wikipedia article, it is called a ray casting algorithm.
The main idea of ββthe algorithm is that you throw a ray in any arbitrary direction from the point you are testing, and calculate how many edges the polygon intersects. If this number is even, then the point lies outside the polygon; otherwise, if it is odd, the point lies inside the polygon.
There are a number of problems with this algorithm that I will not delve into (they are discussed in the Wikipedia article that I linked earlier), but this is the reason why I call this algorithm easy. But in order to give you an idea, you have to handle corner cases using intersecting vertex rays, a ray that runs in parallel and intersects boundary and numerical stability problems with a point lying close to the edge.
You can then use this method as Thomas described in his answer to check if two polygons intersect. This should give you the O(NM) algorithm, where two polygons have vertices N and M respectively.
JPvdMerwe
source share