Click here to Skip to main content
15,799,398 members
Articles / All Topics

Is a Point inside a Polygon?

Rate me:
Please Sign up or sign in to vote.
4.95/5 (13 votes)
9 Mar 2012CPOL3 min read 106.2K   18   3
A short and efficient algorithm for checking point inclusion in a polygon.


The question whether a point is contained within a polygon is a straight-forward one for us to answer visually. However, devising an algorithm that answers this question efficiently and covers most practical cases might still be a little difficult. In this article I will try to describe a short and efficient algorithm named PNPoly by W. Randolph Franklin which solves this problem.


Often when dealing with 2D surfaces, map object or even simple hit tests a fast and efficient implementation is required. Algorithms that address these issues may vary in subset of polygons they are dealing with, whether they are convex, concave, with or without holes and so forth. In this article I will cover the broader case of both convex and concave polygons, but will not address the issue of holes, since it depends on the polygon representation of holes. 

The algorithm

Preliminary Check

First of all and before running this algorithm, it is advisable to perform a simple check that our target point resides outside the bounding rectangle. It's quick and straight-forward to calculate the bounding rectangle and can even be done "offline", while inserting points to the polygon. Then, the check whether our target point is outside the bounding rectangle should look like this:

if (p.x < minX || p.x > maxX || p.y < minY || p.y > maxY) {
    // We're outside the polygon!

where p is our target point and minX/maxX/minY/maxY are the bounding rectangle coordinates. If the above check evaluates to true we can simply return false and avoid further, more costly calculations.

Core algorithm (PNPoly)

Here is an implementation of the algorithm in C (taken from W. Randolph Franklin's website):

<a name="The C Code"></a>int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
  int i, j, c = 0;
  for (i = 0, j = nvert-1; i < nvert; j = i++) {
    if ( ((verty[i]>testy) != (verty[j]>testy)) &&
	 (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
       c = !c;
  return c;

This is it! no additional code, a single loop that iterates over the array of points just once and toggles a flag. But what does it do?!? Let me explain. 

First of all, notice that each iteration considers two adjacent points and the target point. Then the if statement evaluates two conditions:

  1. Y-value of our target point is within the range [verty[j], verty[i]).
  2. X-value of our target point is below the linear line connecting the point j and i.

(If you're having problems to see this second condition, just write down the linear equation of the line, reorganize the expression a little bit and place testy as the free variable).

Every time the above two conditions are met, we toggle the flag c. So we return true if above conditions are met odd number of times and false otherwise. But what does that mean?!?


Let's make the following observation:

a point is within a polygon if and only if its y-value is within the range of the projected polygon on the y-axis and the x-value of the point is below odd number of polygon edges. I will not prove this mathematically, but quick look at few examples will convince yourself that this is true. Notice that this observation is valid for holes too.

Now, if you would go back to the previous section which describes the two conditions you will immediately see that the algorithm is simply checking that the observation is met odd number of times.


Since the algorithm simply iterates through the points collection its runtime complexity is O(n), where n is the number of polygon points. 


Further Reading  

  • Haines, Eric, "Point in Polygon Strategies," Graphics Gems IV, ed. Paul Heckbert, Academic Press, p. 24-46, 1994.
  • Haines, Eric, "Point in Polygon Strategies," web-page.
  • Thread at, Point in Polygon aka hit test, link.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionCopy of the WR Franklin article Pin
Alejandro Xalabarder7-May-20 10:23
Alejandro Xalabarder7-May-20 10:23 
First, a link to the original code and article is missing

PNPOLY - Point Inclusion in Polygon Test - WR Franklin (WRF)[^]

Second, the algorithm is based on the know "Ray casting algorithm" (Point in polygon - Wikipedia[^])

Third, your code (actually WR Franklin code) is not optimized. It does not the check :
"Is the point's Y coordinate within the edge's Y-range?". The code only checks that the edge is not vertical to avoid a division by 0. Testing that the edge might cross the Y target is needed to save multiplication and divisions that are computationally more expensive.
This check is trivial, if both y's of the edge are above or below the target Y then the edge cannot cross the horizontal Y.

And even there are more possible optimizations oriented to avoid the expensive operations * and /.
QuestionFor finding a point on the edge of a polygon Pin
Corin Sandford24-Nov-17 8:43
Corin Sandford24-Nov-17 8:43 
GeneralMy vote of 5 Pin
Joezer BH31-Oct-13 6:49
professionalJoezer BH31-Oct-13 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.