Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geo Fencing - point inside/outside polygon

I would like to determine a polygon and implement an algorithm which would check if a point is inside or outside the polygon.

Does anyone know if there is any example available of any similar algorithm?

like image 295
Niko Gamulin Avatar asked May 29 '09 02:05

Niko Gamulin


People also ask

How do you determine if a point is inside or outside a polygon?

Draw a horizontal line to the right of each point and extend it to infinity. Count the number of times the line intersects with polygon edges. A point is inside the polygon if either count of intersections is odd or point lies on an edge of polygon. If none of the conditions is true, then point lies outside.

What is polygonal geofencing?

Polygonal geofences let you surround only the specific area that you are interested in which improves the data quality and enables precise audience targeting.

How is geofencing calculated?

To do this, just calculate the distance between the center of the circle and your location (longitude, latitude). If the distance is smaller than your circle radius, then you're inside the geofence, otherwise you're outside the geofence. This formula is called the Haversine formula.

How do you check if a given point lies inside or outside a polygon Python?

Use polygon. contains(point) to test if point is inside ( True ) or outside ( False ) the polygon.


2 Answers

If i remember correctly, the algorithm is to draw a horizontal line through your test point. Count how many lines of of the polygon you intersect to reach your point.

If the answer is odd, you're inside. If the answer is even, you're outside.

Edit: Yeah, what he said (Wikipedia):

alt text

like image 93
Ian Boyd Avatar answered Sep 21 '22 14:09

Ian Boyd


C# code

bool IsPointInPolygon(List<Loc> poly, Loc point) {     int i, j;     bool c = false;     for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)     {         if ((((poly[i].Lt <= point.Lt) && (point.Lt < poly[j].Lt))                  || ((poly[j].Lt <= point.Lt) && (point.Lt < poly[i].Lt)))                  && (point.Lg < (poly[j].Lg - poly[i].Lg) * (point.Lt - poly[i].Lt)                      / (poly[j].Lt - poly[i].Lt) + poly[i].Lg))         {              c = !c;         }     }      return c; } 

Location class

public class Loc {     private double lt;     private double lg;      public double Lg     {         get { return lg; }         set { lg = value; }     }      public double Lt     {         get { return lt; }         set { lt = value; }     }      public Loc(double lt, double lg)     {         this.lt = lt;         this.lg = lg;     } } 
like image 45
kober Avatar answered Sep 21 '22 14:09

kober