7
$\begingroup$

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

enter image description here

$\endgroup$

3 Answers 3

12
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ 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). $\endgroup$
    – David K
    Commented Sep 16, 2017 at 23:01
6
$\begingroup$

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.

$\endgroup$
11
  • $\begingroup$ by line A, you mean line AB? And by line B, you mean line CD? $\endgroup$
    – tjvg1991
    Commented Jul 2, 2015 at 23:36
  • 1
    $\begingroup$ A derivation would be nice. $\endgroup$ Commented Jul 2, 2015 at 23:37
  • $\begingroup$ @tjvg1991 Yes, I do, thank you for pointing that out. $\endgroup$
    – Archaick
    Commented Jul 2, 2015 at 23:38
  • 1
    $\begingroup$ 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}$ $\endgroup$
    – Archaick
    Commented Jul 2, 2015 at 23:43
  • 1
    $\begingroup$ 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)$ $\endgroup$
    – user84413
    Commented Jul 3, 2015 at 0:12
2
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ That's the thing. I don't know how to get the normalized vector. $\endgroup$
    – tjvg1991
    Commented Jul 2, 2015 at 23:52
  • $\begingroup$ Do you have the equation of one of the lines? $\endgroup$
    – coldnumber
    Commented Jul 2, 2015 at 23:53
  • $\begingroup$ I think the equation of the line can be derived using the end points of each line, correct? $\endgroup$
    – tjvg1991
    Commented Jul 2, 2015 at 23:57
  • $\begingroup$ 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$ $\endgroup$
    – coldnumber
    Commented Jul 3, 2015 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .