7

Is there a simple way to get 3D distance between two parallel lines given end points of each line?

enter image description here

Archaick
  • 2,273
tjvg1991
  • 356

3 Answers3

11

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:

enter image description here

David
  • 570
  • 3
  • 15
  • 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
5

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.

Archaick
  • 2,273
2

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}.$$

coldnumber
  • 3,721
  • That's the thing. I don't know how to get the normalized vector. – tjvg1991 Jul 02 '15 at 23:52
  • Do you have the equation of one of the lines? – coldnumber Jul 02 '15 at 23:53
  • I think the equation of the line can be derived using the end points of each line, correct? – tjvg1991 Jul 02 '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 03 '15 at 00:22