title: Detect if geolocation is in complex polygon or not
body: <p>We are currently using the following algorithm to detect if a geographic point is inside a complex polygon or not. This works fine, except when the polygon crosses the 180° longitude line.</p>
  
  <p>For example the point (-170, 60) is not detected in polygon 160,65,0 160,15,0 -160,15,0 -160,65,0 160,65,0</p>
  
  <p>Look at the following image: 
  [Img]http://tinypic.com/r/14x2xl1[/img]
  I want everything in the red box. Not the yellow box!</p>
  
  <pre><code>    public static bool IsCoordinateInPolygon(IList&lt;KMLCoordinate&gt; polygon, KMLCoordinate testPoint)
      {
  
          bool result = false;
          int j = polygon.Count - 1;
          for (int i = 0; i &lt; polygon.Count; i++)
          {
              if (polygon[i].Latitude &lt; testPoint.Latitude &amp;&amp; polygon[j].Latitude &gt;= testPoint.Latitude || polygon[j].Latitude &lt; testPoint.Latitude &amp;&amp; polygon[i].Latitude &gt;= testPoint.Latitude)
              {
                  if (polygon[i].Longitude + (testPoint.Latitude - polygon[i].Latitude) / (polygon[j].Latitude - polygon[i].Latitude) * (polygon[j].Longitude - polygon[i].Longitude) &lt; testPoint.Longitude)
                  {
                      result = !result;
                  }
              }
              j = i;
          }
          return result;
  
      }
  </code></pre>
  
  <p>Does anybody have a better algorithm?</p>