determine if the point is in the polygon (theory) To determine whether point P is a very basic but very important algorithm in the computation geometry in the polygon. With point P as the endpoint, to the left as the Ray L, because the polygon is bounded, so the left side of the Ray L must be outside the polygon, considering moving from left to right along L from Infinity, to the first intersection of the polygon, to the interior of the polygon, to the second intersection, to the polygon, ... So it's easy to see that when the number of intersections of L and polygons is odd, p is in the polygon and the even word p is outside the polygon.
But there are special circumstances to consider. As shown in figure (a) (b) (c) (d) below. In figure (a), l and the vertices of a polygon intersect, at which point the intersection can only compute one; in diagram (b), the intersection of L and polygon vertices should not be computed; in graphs (c) and (d), the edges of L and polygons overlap, and this edge should be ignored. If a side of the L and a polygon is coincident, this edge should be ignored.
For the sake of unification, when we compute the intersection of the X ray L and the polygon, 1. The horizontal edges of the polygon are not considered; 2. For the vertex and L intersection of polygons, if the vertex is a larger vertex on the side of which it belongs, the count is ignored; 3. For the case where P is on the edge of a polygon, it can be directly judged that p belongs to the multilateral line. The pseudo code for this algorithm is as follows:
count←0;
Taking p as the endpoint, as the Ray L of the right and left;
For each edge of a polygon s
Do if p on the side s
then return true;
If S is not a horizontal
An endpoint of then if S is on l
If the endpoint is a larger-ordinate endpoint in s at both ends
Then count←count+1
else if S and l intersect
Then count←count+1;
If Count mod 2 = 1
then return true;
else return false;
The method of doing Ray L is to set the ordinate of P ' and P ', and the horizontal axis is positive infinity (a large positive number), then p and P ' determine the X-ray L.
The time complexity of the algorithm for determining whether the point is in a polygon is O (n).
Another algorithm is to use the symbolic area of the triangle and polygon area compared to the algorithm, because of the use of floating point arithmetic so will bring a certain error, do not recommend everyone to use.