title: Polygon contains point algorithm explanation
body: <p>I have seen variants of this solution on many different SO questions about polygons containing a point, but the issue is none of the authors give any explanation.  I cannot seem to figure out how this function works, and seeing that many other commenters have had their questions about this go unanswered, I thought it best to just ask so there would be a concrete explanation.</p>
  
  <p>Also, are there any cases where this function fails? </p>
  
  <p><strong>UPDATE:</strong>
  I do know how the raycasting method works, there are some very good resources for that, but I am really confused how this code works specifically.  </p>
  
  <pre><code>public static bool(ean) PolygonContainsPoint(Point[] polygon, Point point)
  {
      bool(ean) result = false;
      int j = polygon.Count - 1;
      for (int i = 0; i &lt; polygon.Count; i++)
      {
          if (polygon[i].Y &lt; point.Y &amp;&amp; polygon[j].Y &gt;= point.Y || polygon[j].Y &lt; point.Y &amp;&amp; polygon[i].Y &gt;= point.Y)
          {
              if (polygon[i].X + (point.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) &lt; point.X)
              {
                  result = !result;
              }
          }
          j = i;
      }
      return result;
  }
  </code></pre>