3D intersection of a line and a line segment First of all you would be surprise that there's not that much clear solutions for this on stack exchange. I need to know where:


*

*a line, defined by a point $P_0$ and a vector $d$

*and a line segment, defined by two points $A(a_x, a_y, a_z)$ and $B(b_x, b_y, b_z)$


intersects each others.
So far I found this but it's not even for lines.
Let's agree on $\times$ for cross product and $\cdot$ for the dot one.
 A: The line is given by $\{ td+P_0\mid t\in\mathbb R\}$ and the segment by $\{ (1-s)A+sB\mid s\in[0,1]\}$. You need a point in both sets. The easiest way to go about this is to extend the segement into a line by letting $s\in\mathbb R$ instead of just $[0,1]$ and solve linear system $$td+P_0 = (1-s)A + sB$$ for $t$ and $s$. After that, you need to check if $s$ is in $[0,1]$ or not. Also, note that the linear system in 3D is overdetermined, so there might be no solutions at all.
A: For simplicity, let's use
$$\begin{array}{l}
( p_x , p_y , p_z ) = \vec{P}_0 \\
( d_x , d_y , d_z ) = \vec{d} \\
( a_x , a_y , a_z ) = \vec{a} \\
( b_x , b_y , b_z ) = \vec{b}
\end{array}$$
Let's parametrise the line using $s \in \mathbb{R}$ and the line segment using $t \in \mathbb{R}$, $0 \le t \le 1$:
$$\vec{P}_0 + s \vec{d} = (1 - t) \vec{a} + t \vec{b} = \vec{a} + t \left ( \vec{b} - \vec{a} \right)$$
i.e.
$$\begin{cases}
p_x + s d_x = (1 - t) a_x + t b_x \\
p_y + s d_y = (1 - t) a_y + t b_y \\
p_z + s d_z = (1 - t) a_z + t b_z \end{cases}$$
You have three equations, but only two unknowns. Furthermore, because all real $s$ are acceptable, you only need to solve for $t$, and verify it $0 \le t \le 1$. There are three solutions, subscripted by which coordinate pair is used in the solution:
$$t_{xy} = \frac{ d_y ( a_x - p_x ) - d_x ( a_y - p_y ) }{ d_y ( a_x - b_x ) - d_x ( a_y - b_y ) } \tag{1}\label{1}$$
$$t_{xz} = \frac{ d_z ( a_x - p_x ) - d_x ( a_z - p_z ) }{ d_z ( a_x - b_x ) - d_x ( a_z - b_z ) } \tag{2}\label{2}$$
$$t_{yz} = \frac{ d_z ( a_y - p_y ) - d_y ( a_z - p_z ) }{ d_z ( a_y - b_y ) - d_y ( a_z - b_z ) } \tag{3}\label{3}$$
For numerical accuracy, I suggest you calculate all three denominators first, and calculate $t$ using the formula corresponding to the largest denominator in magnitude (absolute value).
If all three denominators are zero, the line and the line segment do not intersect. (This can also occur if $\vec{P}_0 = \vec{d}$ or $\vec{a} = \vec{b}$ or both.)
In case you wish to check for the intersection between two line segments, here are the corresponding formulae for $s$:
$$s_{xy} = \frac{ a_x ( b_y - p_y ) - b_x ( a_y - p_y ) + p_x ( a_y - b_y ) }{ d_y ( a_x - b_x ) - d_x ( a_y - b_y ) } \tag{4}\label{4}$$
$$s_{xz} = \frac{ a_x ( b_z - p_z ) - b_x ( a_z - p_z ) + p_x ( a_z - b_z ) }{ d_z ( a_x - b_x ) - d_x ( a_z - b_z ) } \tag{5}\label{5}$$
$$s_{yz} = \frac{ a_y ( b_z - p_z ) - b_y ( a_z - p_z ) + p_y ( a_z - b_z ) }{ d_z ( a_y - b_y ) - d_y ( a_z - b_z ) } \tag{6}\label{6}$$
Note that the corresponding formulae for $s$ have the same denominators; that is, $\eqref{1}$ and $\eqref{4}$ have the same denominators, $\eqref{2}$ and $\eqref{5}$ have the same denominators, and $\eqref{3}$ and $\eqref{6}$ have the same denominators. (This means that if a solution for $t$ exists, a solution exists for $s$ also. In OP's case $s$ does not matter, because it is the parameter for an infinite line, and any real $s$ is acceptable.)
If you find $t$, and $0 \le t \le 1$, the line and the line segment intersect at $\vec{v}$,
$$\vec{v} = (1-t) \vec{a} + t \vec{b}$$
i.e.
$$\begin{cases}
v_x = (1-t) a_x + t b_x \\
v_y = (1-t) a_y + t b_y \\
v_z = (1-t) a_z + t b_z \end{cases}$$
