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


116 thoughts on “PHP point-in-polygon algorithm

  1. Webidus

    Great and clear article. I’ve implemented in my ‘geo-fence’ project and it works like a charm !!! If I could give it stars , I would give you 5/5 stars 🙂

    Reply
  2. Fernando

    thank you so mucho… you saved me a lot of time. My project is about Google maps polygons and i should found region in vaious that a point is inside. Thanks, you are the man!

    Reply
  3. esoares9483

    For whoever else had this problem, it took me about three weeks to realize my rookie mistake.
    When polygons from the database will not work but works when pasted in your file it is because the polygon is a string and not an actual array. This is a simple way i fixed that problem.

    The example is CodeIgniter blah blah, just explode your string you get from MySQL, I did not even have to use array() either, in fact I think it broke it when pulling from database.

    $polygons = $this->location_model->get_geoCoordinates($location_id);
    $polygon = explode(‘”,”‘,$polygons);

    In closing let me know if you get stuck.

    Reply
  4. Parveen

    Hi,

    I getting wrong response. Function giving response “inside”, but when I checked at google given point is not exit in given area.

    Points are :

    $points = array(“30.71102842 76.70939199”);
    $polygon = array(“30.711064231914783 76.7093163728714″,”30.711140331172867, 76.7094960808754″,”30.711008886962087, 76.7096221446991″,”30.71093509364243, 76.70941829681396”);

    Please reply me as soon as possible.

    Thanks,

    Parveen

    Reply
    1. Michael Post author

      Hi Parveen,
      As mentioned in the article, the polygon must be “closed”, that is, the first and last point must be identical. This is likely the cause of the wrong response. Just duplicate the first point’s coordinates to the end of the polygon array.
      Regards,
      Michael

      Reply

Leave a comment