All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 Intersecting lines (Posted on 2003-07-01)
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)

 See The Solution Submitted by levik Rating: 4.0000 (2 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
 re: Solution | Comment 6 of 20 |
(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

 Search: Search body:
Forums (0)