#### Archived

This topic is now archived and is closed to further replies.

# Two (2) Line Segments intersect detection boolean response

This topic is 5327 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites

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?

##### Share on other sites
... and this can be optimized as you should see by avoiding the unecessary divisions. You need a samesign() routine.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633004
• Total Posts
3009836
• ### Who's Online (See full list)

There are no registered users currently online

×