tags:

views:

238

answers:

2

Lets say we have 4 Vector2's (yes this is 2d), so we have lineOneStart, lineOneEnd, lineTwoStart and lineTwoEnd.

How can I detect if the 2 lines cross? I don't care where they cross, I just want to know if they intersect.

+5  A: 

Check this formula by Bourke.

I recently had to solve this issue too. Another option is using (getting) the equation of the line (y = mx + c) but there are several edge cases you need to be concerned with, as well as actually checking if the point of intersection is within the line segment. The formula in the link above works, though I cannot really comment on how the equation is re-arranged, all I'll say is it works ;)

Edit:

As mentioned by AndiDog, another site I used (the example is excellent too) is this tutorial. As this is XNA the second link will be right up your street.

Finglas
+1 for citing Bourke's collection of geometry algorithms. I use it all the time, it's great.
David Seiler
+1  A: 

There's a tutorial on that topic (line segment intersection).

AndiDog