# How to get a projected 3d line segment, lie on another 3d line parallel to that line segment.

I have a 3D line segment and another 3D position which locate slightly away from the line segment. I want to get the projected line segment (3D) which lies on imaginary 3D line which passes through the mentioned 3D position and parallel to that line segment. I am bit confused to find a way to do it. Any directions please. Thanks.

-

So let's say you have a line segment represented by two end points $P_1$ and $P_2$ and an additional point $Q$. We first compute $Q$'s shortest distance vector to the line, meaning the vector that points from the line to $Q$ and is orthogonal to the line.
For this we first take the line vector $\mathbf{p}=P_2-P_1$ and the vector $\mathbf{q}=Q-P_1$ from $P_1$ to $Q$. We then only need to subtract that part from $\mathbf{q}$ that is parallel to the line ($\mathbf{p}$):
$$\mathbf{v}=\mathbf{q}-\frac{\mathbf{q}\cdot\mathbf{p}}{\|\mathbf{p}\|^2}\mathbf{q}$$
And this vector, whose euclidean norm equals $Q$'s shortest distance from the line, is the vector we need to translate our line by:
$$P'_1=P_1+\mathbf{v},\qquad P'_2=P_2+\mathbf{v}$$