[Math] How to test any 2 line segments (3D) are collinear or not

computational geometrygeometryvector-spaces

if we have two line segments in 3D, what would be the way to test whether these two lines are collinear or not? (I fogot to mentioned that my line segments are 3D. So, I edited the original post. Sorry for the inconveniences)
I wish to check the direction of the lines and the perpendicular distance between them.
Does these two factors are enough to decide whether 2 line segments are collinear or not.
Thank you in advance.

Best Answer

An alternative method. Assume $PQ$ and $RS$ are the line segments. Let the direction cosines of the vectors $\mathbf{u=} \overrightarrow{PQ}$ and $\mathbf{v=}\overrightarrow{RS}$ be, respectively, $ \alpha _{u},\beta _{u},\gamma _{u}$ and $\alpha _{v},\beta _{v},\gamma _{v}$. The angle $\phi $ between the line segments is such that$^{1}$ $$ \begin{equation*} \cos \phi =\alpha _{u}\alpha _{v}+\beta _{u}\beta _{v}+\gamma _{u}\gamma _{v}. \end{equation*} $$

Hence the line segments are collinear if $\cos \phi =\pm 1$.

--

$^{1}$Formula 10.7 of Manual de Fórmulas e Tabelas Matemáticas, Coleção Schaum, Portuguese translation of Schaum's Outline Series Mathematical Handbook of Formulas and tables, 2/e by Murray Spiegel and John Liu.