PHP point-in-polygon algorithm

The point-in-polygon algorithm allows you to programmatically check if a particular point is inside a polygon or outside of it. A common way to tackle the problem is to count how many times a line drawn from the point (in any direction) intersects with the polygon boundary. If the line and the polygon intersect an even number of times (or not at all), then the point is outside. If they intersect an odd number of times, the point is inside. It is true even for complex forms that have a lot of coordinates and thus create a very precise boundary.

Let’s see a sample image before we get to the code.

This shows how it can be determined if a point is inside or outside a polygon.Here, the lines drawn from point 1 intersect twice or not at all, because it is outside.

Point 2 is inside and thus the lines drawn from it intersect once or three times.

Even in special cases, such as point 3, we see that this method works: the line intersects twice and the point is therefore outside.

I use this approach in the PHP code below, which returns one of these 4 possible values:

  • inside if the point is inside the polygon.
  • outside if, you guessed it, the point is outside of the polygon.
  • vertex if the point sits exactly on a vertex AND $pointOnVertex = true (line 2)
  • boundary if the point sits on the boundary. If $pointOnVertex = false, then boundary is also returned if the point is on a vertex.

Using the point-in-polygon PHP code

Set the point(s) value(s) and an array containing your polygon vertices (in the form “Xcoordinate Ycoordinate”), then call the pointInPolygon function. The first and last polygon coordinates must be identical, to “close the loop”.

As you can see in the following example, it is easy to check multiple points at once. The code also works with negative coordinates, for the polygon as well as for the points to check.

Point in polygon - sampleThis will output:

point 1 (50 70): vertex
point 2 (70 40): inside
point 3 (-20 30): inside
point 4 (100 10): outside
point 5 (-10 -10): outside
point 6 (40 -20): inside
point 7 (110 -20): boundary

5 thoughts on “PHP point-in-polygon algorithm

  1. MrDog

    Very nice and saved me a lot of work. But one query – you say the last point in the polygon must be the same as the first one to close the loop which I understand – but the first one is (-50,30) and the last one is (-50,-30) which are not the same – which implies to me that the last value is not really needed?

    Reply
    1. Michael Post author

      Thanks for pointing that out!
      It was a mistake (now corrected) in the sample. The last coordinate does have to be the same as the first one.

      Regards

      Reply
  2. Rhijul

    Is there a way to optimise the code to run the algorithm for searching multiple points. Basically if we can get the status of all the points whether they are inside or outside. I know we can run the same algorithm over all the points, but then the we will be repeating a lot of our intersection calculations and the algorithm would not be that efficient.

    Reply

Leave a comment