A
necessary condition for two lines to intersect is that they are in the same plane—that is, are not skew lines. Satisfaction of this condition is equivalent to the
tetrahedron with vertices at two of the points on one line and two of the points on the other line being
degenerate in the sense of having zero
volume. For the algebraic form of this condition, see .
Given two points on each line First we consider the intersection of two lines and in two-dimensional space, with line being defined by two distinct points and , and line being defined by two distinct points and . The intersection of line and can be defined using
determinants. : P_x = \frac{\begin{vmatrix} \begin{vmatrix} x_1 & y_1\\x_2 & y_2\end{vmatrix} & \begin{vmatrix} x_1 & 1\\x_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & y_3\\x_4 & y_4\end{vmatrix} & \begin{vmatrix} x_3 & 1\\x_4 & 1\end{vmatrix} \end{vmatrix} } {\begin{vmatrix} \begin{vmatrix} x_1 & 1\\x_2 & 1\end{vmatrix} & \begin{vmatrix} y_1 & 1\\y_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & 1\\x_4 & 1\end{vmatrix} & \begin{vmatrix} y_3 & 1\\y_4 & 1\end{vmatrix} \end{vmatrix}}\,\! \qquad P_y = \frac{\begin{vmatrix} \begin{vmatrix} x_1 & y_1\\x_2 & y_2\end{vmatrix} & \begin{vmatrix} y_1 & 1\\y_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & y_3\\x_4 & y_4\end{vmatrix} & \begin{vmatrix} y_3 & 1\\y_4 & 1\end{vmatrix} \end{vmatrix} } {\begin{vmatrix} \begin{vmatrix} x_1 & 1\\x_2 & 1\end{vmatrix} & \begin{vmatrix} y_1 & 1\\y_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & 1\\x_4 & 1\end{vmatrix} & \begin{vmatrix} y_3 & 1\\y_4 & 1\end{vmatrix} \end{vmatrix}}\,\! The determinants can be written out as: : \begin{align} P_x&= \frac{(x_1 y_2-y_1 x_2)(x_3-x_4)-(x_1-x_2)(x_3 y_4-y_3 x_4)}{(x_1-x_2) (y_3-y_4) - (y_1-y_2) (x_3-x_4)} \\[4px] P_y&= \frac{(x_1 y_2-y_1 x_2)(y_3-y_4)-(y_1-y_2)(x_3 y_4-y_3 x_4)}{(x_1-x_2) (y_3-y_4) - (y_1-y_2) (x_3-x_4)} \end{align} When the two lines are parallel or coincident, the denominator is zero.
Given two points on each line segment The intersection point above is for the infinitely long lines defined by the points, rather than the
line segments between the points, and can produce an intersection point not contained in either of the two line segments. In order to find the position of the intersection with respect to the line segments, we can define lines and in terms of first degree
Bézier parameters: : L_1 = \begin{bmatrix}x_1 \\ y_1\end{bmatrix} + t \begin{bmatrix}x_2-x_1 \\ y_2-y_1\end{bmatrix}, \qquad L_2 = \begin{bmatrix}x_3 \\ y_3\end{bmatrix} + u \begin{bmatrix}x_4-x_3 \\ y_4-y_3\end{bmatrix} (where and are real numbers). The intersection point of the lines is found with one of the following values of or , where : t = \frac{\begin{vmatrix} x_1-x_3 & x_3-x_4\\y_1-y_3 & y_3-y_4\end{vmatrix}}{\begin{vmatrix} x_1-x_2 & x_3-x_4\\y_1-y_2 & y_3-y_4\end{vmatrix}} = \frac{(x_1 - x_3)(y_3-y_4)-(y_1-y_3)(x_3-x_4)}{(x_1-x_2)(y_3-y_4)-(y_1-y_2)(x_3-x_4)} and : u = - \frac{\begin{vmatrix} x_1-x_2 & x_1-x_3\\y_1-y_2 & y_1-y_3\end{vmatrix}}{\begin{vmatrix} x_1-x_2 & x_3-x_4\\y_1-y_2 & y_3-y_4\end{vmatrix}} = -\frac{(x_1 - x_2)(y_1-y_3)-(y_1-y_2)(x_1-x_3)}{(x_1-x_2)(y_3-y_4)-(y_1-y_2)(x_3-x_4)}, with : (P_x, P_y)= \bigl(x_1 + t (x_2-x_1),\; y_1 + t (y_2-y_1)\bigr) \quad \text{or} \quad (P_x, P_y) = \bigl(x_3 + u (x_4-x_3),\; y_3 + u (y_4-y_3)\bigr) There will be an intersection if and . The intersection point falls within the first line segment if , and it falls within the second line segment if . These inequalities can be tested without the need for division, allowing rapid determination of the existence of any line segment intersection before calculating its exact point. In the case where the two line segments share an x axis and x_2 = x_1 + 1 , t and u simplify to t = u = \frac{y_1 - y_3}{y_1 - y_2 - y_3 + y_4}, with (P_x, P_y)= \bigl(x_1 + t,\; y_1 + t (y_2-y_1)\bigr) \quad \text{or} \quad (P_x, P_y) = \bigl(x_1 + t,\; y_3 + t (y_4-y_3)\bigr).
Given two line equations The and coordinates of the point of intersection of two non-vertical lines can easily be found using the following substitutions and rearrangements. Suppose that two lines have the equations and where and are the
slopes (gradients) of the lines and where and are the -intercepts of the lines. At the point where the two lines intersect (if they do), both coordinates will be the same, hence the following equality: :ax+c = bx+d. We can rearrange this expression in order to extract the value of , :ax-bx = d-c, and so, :x = \frac{d-c}{a-b}. To find the coordinate, all we need to do is substitute the value of into either one of the two line equations, for example, into the first: :y = a\frac{d-c}{a-b}+c. Hence, the point of intersection is :P = \left( \frac{d-c}{a-b}, a\frac{d-c}{a-b}+c \right) . Note that if then the two lines are
parallel and they do not intersect, unless as well, in which case the lines are coincident and they intersect at every point.
Using homogeneous coordinates By using
homogeneous coordinates, the intersection point of two implicitly defined lines can be determined quite easily. In 2D, every point can be defined as a projection of a 3D point, given as the ordered triple . The mapping from 3D to 2D coordinates is . We can convert 2D points to homogeneous coordinates by defining them as . Assume that we want to find intersection of two infinite lines in 2-dimensional space, defined as and . We can represent these two lines in
line coordinates as and . The intersection of two lines is then simply given by :P' = (a_p, b_p, c_p) = U_1 \times U_2 = (b_1 c_2 - b_2 c_1, a_2 c_1-a_1 c_2, a_1 b_2 - a_2 b_1) If , the lines do not intersect. == More than two lines ==