# finding points on the line connecting two 3D points

I have two points in 3D space. Say these are P = (x, y, z) and Q = (u, v, w). Now, I would like to get line segments from P to Q of length epsilon each. So, for that, I need all the points on the line from P to Q that are apart by length epsilon. I can do this for a line in 2D, but I am confused with how to do this for a line in 3D?

Here is what I tried.

Set current point to be P and the next point to be current point + epsilon * (Q-P)/|Q-P|.

Z = {current point, next point}

Then, iteratively, went through adding to the set Z, next point + epsilon * (Q-P)/|Q-P|.

However, I can not figure out how to stop this iterative process.

• Find unit vector $\hat{u}$ in the direction $PQ$, then the other end of the line segment is $~ P + \epsilon ( \hat{u})$ Oct 9, 2021 at 16:04
• Should the unit vector in the direction of PQ simply be (Q - P)/norm(Q - P)? How do I ensure that the segments do not go out of the line connecting the points PQ? Oct 9, 2021 at 16:06
• unit vector? Yes $(Q-P) / |Q-P|$ Oct 9, 2021 at 16:08
• I tried this, but I can not figure out how to check that it does not go beyond Q. If you write it as an answer, I can "checkmark" it. Oct 9, 2021 at 16:09
• But that depends on $\epsilon$. If you want the point to be between $P$ and $Q$ then you must ensure $\epsilon \leq |Q-P|$ Oct 9, 2021 at 16:11

The number of line segments ($$n$$) of length $$\epsilon$$ starting from point $$P$$ and ending at or before point $$Q$$ must be,
$$\displaystyle n = \left \lfloor \frac{|\textbf{Q}-\textbf{P}|}{\epsilon} \right \rfloor$$
At the start of the iteration, $$\textbf{P}_{1} = \textbf{P}$$
Iterate for $$1 \leq m \leq n$$,
$$\displaystyle \small \textbf{Q}_m = \textbf {P}_m + \frac{\textbf{Q}-\textbf{P}}{|\textbf{Q}-\textbf{P}|} \epsilon ~$$
and $$\textbf{P}_{m+1} = \textbf{Q}_{m}$$