title: Does this polyline pass through this polygon?
body: <p>I’m trying to test whether a Google Maps polyline passes through a Google Maps polygon. Sounds simple. But I’ve searched and searched... and found no real answers.</p>
  
  <p>Closest I got was this function. It works but, frustratingly, returns the occasional false positive.</p>
  
  <pre><code>//nvert = the number of points in the polygon
  //vertx = an array of all the polygon's latitudes
  //verty = an array of all the polygon's longitudes
  //elat = the current point's latitude
  //elng = the current point's longitude
  
  function pnpoly( nvert, vertx, verty, elat, elng) {
          var i, j, c = false;
          for( i = 0, j = nvert-1; i &lt; nvert; j = i++ ) {
              if( ( ( verty[i] &gt; elng ) != ( verty[j] &gt; elng ) ) &amp;&amp;
                  ( elat &lt; ( vertx[j] - vertx[i] ) * ( elng - verty[i] ) / ( verty[j] - verty[i] ) + vertx[i] ) ) {
                      c = !c;
              }
            }
            return c;
      }
  </code></pre>
  
  <p>Before I try a whole new method (a <a href="http://zonalandeducation.com/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.html" rel="nofollow">crazy math idea</a> that brings me back to Grade 12 calculus), I’m wondering anyone knows how to accomplish this.</p>