There are many problems where one needs to check if a point lies completely inside a convex polygon. Before moving into the solution of this problem, let us first check if a point lies left or right of a line segment. A polygon consists of more than two line segments ordered in a clockwise or anti-clockwise fashion. If a point lies left (or right) of all the edges of a polygon whose edges are in anticlockwise (or clockwise) direction then we can say that the point is completely inside the polygon.
Consider a line segment $(a, b)$ given in the figure below. The coordinates of the end points of the line segments are $(x_1, y_1)$ and $(x_2, y_2)$ respectively. Consider a point $p(x, y)$ somewhere on the $xy$ plane.
To check if the point $p(x, y)$ lies on the left or on the right of the line segment $(a, b)$, we first express the equation of the line segment in the following format.
$$Ax + By + C = 0$$
The values of $A, B$ and $C$ can be calculated using the end points coordinates as $A = -(y_2 - y_1)$, $B = x_2 - x_1$ and $C = -(Ax_1 + By_1)$. A point $p(x, y)$ lies on the left of the line segment given by $A, B$ and $C$ if,
$$Ax + By + C > 0$$
Similarly, the point $p(x, y)$ lies on the right of the line if,
$$Ax + By + C < 0$$
Finally, the point lies exactly on the line if it satisfies the following relation.
$$Ax + By + C = 0$$
Now we know how to check if a point lies on the left of a line segment. Since a polygon is a combination of more than two line segments (or edges), we check if the point lies on the left of the each edge (or we check if the point lies on the right of the each edge if the edges are in clockwise direction). Consider a polygon $abcdefa$ and a point $p$ given in the figure below.
In order for the point $p$ to be completely inside the polygon $abcdefa$, it must lie on the left of edges $ab$, $bc$, $cd$, $de$, $ef$ and $fa$.
So far we have discussed the mathematical solution of the problem. The same problem can be solved programmatically using any programming language. In this article, I use python programming language but the implementation is nearly same for almost all programming languages.
Data structure to represent a point is sufficient for this problem. We do not need a data structure for a line segment for this particular problem since the two extreme points (end points) are sufficient to represent a line segment. A point consists of two components, x-coordinate and y-coordinate. x and y coordinates are sufficient to uniquely represent a point in a 2D plane. The following python code snippet creates a point data structure.
An instance of the Point can be easily created by calling the constructor as
Point(3,4) which creates a point with coordinates $(3, 4)$
If the polygon is not convex, the above trick does not work. Consider a polygon given below.
As you can see, the polygon is not a convex polygon. We want to know if a point $p$ is inside the polygon. In this case, we do the following
- We draw a horizontal ray originating from the point $p$ and extend it towards infinity in the right direction as shown in the figure above.
- We count the number of intersection the ray makes with the edges of the polygon.
- If number of intersection is even, the point is outside the polygon otherwise it is inside the polygon.
There are two special (degenerate) cases we need to address.
If an edge of the polygon lie along the ray, we ignore that edge (we do not count this as an intersection). This is illustrated in the figure below.
If the ray passes through the vertex of the polygon (as shown in figure below), only count the edge whose other vertex lies below the ray.
- Finding whether a point lies inside a rectangle or not. (2010, May 2). Retrieved August 19, 2018, from https://stackoverflow.com/questions/2752725/finding-whether-a-point-lies-inside-a-rectangle-or-not