# Find 3D distance between two parallel lines in simple way

Is there a simple way to get 3D distance between two parallel lines given end points of each line? Simplest way in my opinion: You can easily calculate the unit direction vector $v$ in each line (subtract the two end points and then divide by the distance between them). $v$ is the same for both lines since they are parallel.

Now we say that $line1$ is represented by a point $p1$ and a unit vector $v$. and $line2$ is represented by a point $p2$ and the same unit vector $v$. Then in this case the distance between $line1$ and $line2$ is $||v \times (p_2-p_1)||$. You can see why in the drawing below: • I notice the formula at the end is the same as in one of the previous answers, though this answer did contribute a justification for the formula (with a useful diagram). – David K Sep 16 '17 at 23:01

Indeed. Let $\textbf{p}_1$ and $\textbf{p}_2$ be points on line $A$ and $\textbf{q}$ be a point on line $B$. Then $d(A,B)$, the distance between lines $A$ and $B$ can be quickly found using the expression $$d(A,B)=\left|(\textbf{q}-\textbf{p}_1)-\frac{(\textbf{q}-\textbf{p}_1)\cdot(\textbf{p}_2-\textbf{p}_1)}{|\textbf{p}_2-\textbf{p}_1|^2}(\textbf{p}_2-\textbf{p}_1)\right|$$

The reasoning here is that we want to take a vector which connects the two lines and decompose it into the component which is orthogonal to the lines and the component which is parallel. We therefore take the orthogonal projection (the component which is parallel to the lines) of the vector $\textbf{q}-\textbf{p}_1$ and subtract it from $\textbf{q}-\textbf{p}_1$ to find the vector we were looking for (the one orthogonal to the lines which connects them). We are interested in the magnitude of this vector and so we take the magnitude of this vector.

Good question! Let me know if the reasoning here is in any way unclear.

• by line A, you mean line AB? And by line B, you mean line CD? – tjvg1991 Jul 2 '15 at 23:36
• A derivation would be nice. – marty cohen Jul 2 '15 at 23:37
• @tjvg1991 Yes, I do, thank you for pointing that out. – Archaick Jul 2 '15 at 23:38
• The bars mean the magnitude (the length) of the vector. That is, if $\textbf{v}=(v_1,v_2,v_3)$, then $|\textbf{v}|=\sqrt{(v_1)^2+(v_2)^2+(v_3)^2}$ – Archaick Jul 2 '15 at 23:43
• I may be wrong about this, but I think you want to multiply the second term on the right side by $(p_2-p_1)$ – user84413 Jul 3 '15 at 0:12

If you know the lines are parallel, you can solve the problem using the formula for the distance between a point and a line: form a vector from a point on the first line to a point on the second line and cross it with the normalized direction vector of one of the lines.

I.e., if $P$ is on the first line, $Q$ is on the second line, and $u$ is the normalized direction vector of one of the lines (so it has unit length), the distance between the lines is $\lVert PQ \times u \rVert$.

EDIT: if the direction vector does not have unit length, the formula is $$\frac{\lVert PQ \times u\rVert}{\lVert u\rVert}.$$

• That's the thing. I don't know how to get the normalized vector. – tjvg1991 Jul 2 '15 at 23:52
• Do you have the equation of one of the lines? – coldnumber Jul 2 '15 at 23:53
• I think the equation of the line can be derived using the end points of each line, correct? – tjvg1991 Jul 2 '15 at 23:57
• Yes, if you have two points of the line, you can form a vector that lies on the line (just subtract the coordinates). That's your direction vector, call it $v$. (Note that parallel lines have parallel direction vectors.) If you don't want to normalize it, you can find the distance with $\lVert PQ \times v\rVert / \lVert v \rVert$ – coldnumber Jul 3 '15 at 0:22