Jump to content
  • Advertisement

Archived

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

HaHa

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!