I\'m using .NET to make an application with a drawing surface, similar to Visio. The UI connects two objects on the screen with Graphics.DrawLine. This simple implementation wor
If you rotate your frame of reference to align with the first line segment (so the origin is now the start of the first line, and the vector for the first line extends along the X-axis) the question becomes, where does the second line hit the X-axis in the new coordinate system. This is a much easier question to answer. If the first line is called A
and it is defined by A.O
as the origin of the line and 'A.V' being the vector of the line so that A.O + A.V
is the end point of the line. The frame of reference can be defined by the matrix:
| A.V.X A.V.Y A.O.X |
M = | A.V.Y -A.V.X A.O.Y |
| 0 0 1 |
In homogeneous coordinates this matrix provides a basis for the frame of reference that maps the line A
to 0 to 1 on the X-axis. We can now define the transformed line B
as:
C.O = M*(B.O)
C.V = M*(B.O + B.V) - C.O
Where the *
operator properly defined for homogeneous coordinates (a projection from 3 space onto 2 space in this case). Now all that remains is to check and see where C
hits the X-axis which is the same as solving Y
side of the parametric equation of C
for t
:
C.O.Y + t * C.V.Y = 0
-C.O.Y
t = --------
C.V.Y
If t
is in the range 0 to 1, then C
hits the X-axis inside the line segment. The place it lands on the X-axis is given by the X side of the parametric equation for C
:
x = C.O.X + t * C.V.X
If x
is in the range 0 to 1 then the intersection is on the A
line segment. We can then find the point in the original coordinate system with:
p = A.O + A.V * x
You would of course have to check first to see if either line segment is zero length. Also if C.V.Y = 0
you have parallel line segments. If C.V.X
is also zero you have colinear line segments.