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
Solution by friedlinguini)
When I use
Ax, Ay, Bx, By, Cx, Cy, Dx, Dy equal to
1,3,4,1,3,1,5,3
so that AB is from (1,3) to (4,1) and CD is from (3,1) to (5,3), which intersect,
I get the following values:
By * Cx - Ay * Cx + Ax * Cy - By * Cy + Ay * Bx - Ax * By = 5
By * Dx - Ay * Dx + Ax * Dy - By * Dy + Ay * Bx - Ax * By = 1
Dy * Ax - Cy * Ax + Cx * Ay - Dy * Ay + Cy * Dx - Cx * Dy = -2
Dy * Bx - Cy * Bx + Cx * By - Dy * By + Cy * Dx - Cx * Dy = 4
Then, while 5*1 > 0, (-2)*4 < 0 so the ANDed condition fails. What went wrong here?
|
Posted by Charlie
on 2003-07-02 03:40:46 |