Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool intersects(LevelEdge p_e)
- {
- //http://flassari.is/2008/11/line-line-intersection-in-cplusplus/
- float x1 = p1.x, x2 = p2.x, x3 = p_e.p1.x, x4 = p_e.p2.x;
- float y1 = p1.y, y2 = p2.y, y3 = p_e.p1.y, y4 = p_e.p2.y;
- float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
- if (d == 0) return false; // no intersection
- // get the x and y
- float pre = (x1 * y2 - y1 * x2), post = (x3 * y4 - y3 * x4);
- float x = (pre * (x3 - x4) - (x1 - x2) * post) / d;
- float y = (pre * (y3 - y4) - (y1 - y2) * post) / d;
- // Check if the x and y coordinates are within both lines
- if (x < fmin(x1, x2) || x > fmax(x1, x2) ||
- x < fmin(x3, x4) || x > fmax(x3, x4)) return false;
- if (y < fmin(y1, y2) || y > fmax(y1, y2) ||
- y < fmin(y3, y4) || y > fmax(y3, y4)) return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement