Skip to main content
gamedev.net
🔒 Locked

2D Line Intersection

Started by Barguast 1 reply 300+ views
Original Post
Barguast
Barguast
Yes, I''ve searched for this, but they all semm to tell me more than I need to know. Basically, I have two lines v1-v2 and v3-v4. I just need a simple test to say whether they cross each other or not. Thanks
Using Visual C++ 7.0
aldisd
aldisd
google for "intersection of two lines"
oliii
oliii
something like that?


// ''plane'' from segment 1 (normal is perpendicular to segment)

Vector n0(v1.y - v2.y, v2.x - v1.x);
float d0 = v1.x*n0.x + v1.y*n0.y;

// ''plane'' from segment 2 (normal is perpendicular to segment)

Vector n1(v3.y - v4.y, v4.x - v3.x);
float d1 = v3.x*n1.x + v3.y*n1.y;

// sign of v1 and v2 from segment plane 2 (dot product)

float s1 = (v1.x*n1.x + v1.y*n1.y) - d1;
float s2 = (v2.x*n1.x + v2.y*n1.y) - d1;

// sign of v3 and v4 from segment plane 1

float s3 = (v3.x*n0.x + v3.y*n0.y) - d0;
float s4 = (v4.x*n0.x + v4.y*n0.y) - d0;

// segments don''t cross a plane, so they can''t intersect

if (sign(s1) == sign(s2)) return false;
if (sign(s3) == sign(s4)) return false;

return true;

Everything is better with Metal.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.