title: Polygon-Line Collision Detection
body: <p>I am trying to find a formula to determine if a line intersects a polygon.  I have tried, but the code below is not working properly.</p>
  
  <pre><code>bool Check_Collision(float x1,float y1, float x2, float y2)
  {
          int j=MyPolyVector.size()-1;
          for (int i=0;i&lt;MyPolyVector.size();i++)
          {
                  float x3=MyPolyVector[i].X;
                  float x4=MyPolyVector[j].X;
                  float y3=MyPolyVector[i].Y;
                  float y4=MyPolyVector[j].Y;
  
                  float denom= ((y4-y3)*(x2-x1))-((x4-x3)*(y2-y1));
                  float ua = (((x4-x3)*(y1-y3))-((y4-y3)*(x1-x3)))/denom;
                  float ub = (((x2-x1)*(y1-y3))-((y2-y1)*(x1-x3)))/denom;
                  j=i;
  
                  if(ua &gt;= 0.0f &amp;&amp; ua &lt;= 1.0f &amp;&amp; ub &gt;= 0.0f &amp;&amp; ub &lt;= 1.0f) return true;
          }
          return false;
  }
  </code></pre>