Two (2) Line Segments intersect detection boolean response
Hey,
I was just wondering if anyone could help me figure out a simple intersect detection problem I''m having between two (2) line segments. I don''t want the intersection point just a boolean response if the line segments are overlapping or not.
I have seen plenty of resources on how to do this test in 3D and/or find the exact point of intersection. I have extracted a boolean response from other formulas but I am seeking a cheap simple boolean check.
Thanks for any help you could provide me!
bool LineSegmentLineSegmentIntersection( Vector2D l1p1, Vector2D l1p2, Vector2D l2p1, Vector2D l2p2){ Vector2D p1 = l1p1; Vector2D s1 = l1p2 - l1p1; Vector2D p2 = l2p1; Vector2D s2 = l2p2 - l2p1; float s = ( -s1.y * (p1.x - p2.x) + s1.x * (p1.y - p2.y) ) / (-s2.x * s1.y + s1.x * s2.y); float t = ( s2.x * (p1.y - p2.y) - s2.y * (p1.x - p2.x)) / (-s2.x * s1.y + s1.x * s2.y); if (s >= 0 && s <= 1) { if (t >=0 && t<=1) { return true; } } return false;}
That what you are looking for?
... and this can be optimized as you should see by avoiding the unecessary divisions. You need a samesign() routine.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement