Advertisement
Guest User

Untitled

a guest
Apr 11th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.79 KB | None | 0 0
  1. bool intersects(LevelEdge p_e)
  2. {
  3.     //http://flassari.is/2008/11/line-line-intersection-in-cplusplus/
  4.     float x1 = p1.x, x2 = p2.x, x3 = p_e.p1.x, x4 = p_e.p2.x;
  5.     float y1 = p1.y, y2 = p2.y, y3 = p_e.p1.y, y4 = p_e.p2.y;
  6.    
  7.     float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
  8.     if (d == 0) return false; // no intersection
  9.    
  10.     // get the x and y
  11.     float pre = (x1 * y2 - y1 * x2), post = (x3 * y4 - y3 * x4);
  12.     float x = (pre * (x3 - x4) - (x1 - x2) * post) / d;
  13.     float y = (pre * (y3 - y4) - (y1 - y2) * post) / d;
  14.    
  15.     // Check if the x and y coordinates are within both lines
  16.     if (x < fmin(x1, x2) || x > fmax(x1, x2) ||
  17.         x < fmin(x3, x4) || x > fmax(x3, x4)) return false;
  18.     if (y < fmin(y1, y2) || y > fmax(y1, y2) ||
  19.         y < fmin(y3, y4) || y > fmax(y3, y4)) return false;
  20.    
  21.     return true;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement