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)
If line segments AB and CD intersect, then points C and D will fall on opposite sides of AB (or one will fall directly on AB), and similarly, points A and B will fall on eith side of (or on) CD. Therefore, one means of determining if line segments AB and CD intersect is as follows:
- Convert all (x,y) coordinates to (r,θ) coordinates relative to point A.
- For AB to intersect CD, one requirement is for θb to be between θc and θd, inclusive. If not, the line segments do not intersect.
- Next, convert all (x,y) coordinates to (r,θ) coordinates relative to point C.
- If θd is between θa and θb, inclusive, the line segments intersect.
It is a straight-forward exercise to write an algorithm to perform this function, so one is not included here.
|
Posted by Bryan
on 2003-07-01 12:27:50 |