You are given two straight line segments, each defined by the coordinates of its endpoints. Segment AB goes from (Ax,Ay) to (Bx,By) and segment CD - from (Cx,Cy) to (Dx,Dy).
How would you determine if the two line segments intersect?
(Assume that you can't just draw the lines and see)
(In reply to
re: Solution by Charlie)
It's because I messed up in the fourth term of each of the factors. I also switched the comparison operators. The corrected (again!) result is:
return (By*Cx - Ay*Cx + Ax*Cy - Bx*Cy + Ay*Bx - Ax*By) *
(By*Dx - Ay*Dx + Ax*Dy - Bx*Dy + Ay*Bx - Ax*By) <= 0
&& (Dy*Ax - Cy*Ax + Cx*Ay - Dx*Ay + Cy*Dx - Cx*Dy) *
(Dy*Bx - Cy*Bx + Cx*By - Dx*By + Cy*Dx - Cx*Dy) <= 0;
This gives the expected result.