[Math] Position of point between 2 points in 3D space

3dcoordinate systemsgeometry

I need to find the position v3 between the given points v1, and v2 and a given distance d in 3D space.

I came across this post:
Position of point between 2 points

which is basically what I need but in 2D. I have tried this equation:
$$
t = \left(\sqrt{ (v2_x – v1_x)^2 + (v2_y – v1_y)^2 + (v2_z – v1_z)^2 } \right) -d
$$
then for v3
$$
v3_x= \frac{(v2_x)d + (v1_x)t}{d+t},v3_y= \frac{(v2_y)d + (v1_y)t}{d+t},v3_z= \frac{(v2_z)d + (v1_z)t}{d+t}
$$

However this is wrong, because when I plug this into my 3D application I get the wrong values for v3.

Can anyone tell where I'm going wrong or point me in the right direction? (I'm not great at maths)

Best Answer

To be clear: you've got two points, $v_1$ and $v_2$ which are some distance apart -- say 11 units. You want a point that's partway from $v_1$ to $v_2$, say, 5 units. That "5" is your $d$, right?

If so, here's your answer: $$ u = \sqrt{ (v2_x - v1_x)^2 + (v2_y - v1_y)^2 + (v2_z - v1_z)^2 } \\ s = \frac{d}{u}\\ v_3 = v1 + s(v_2 - v_1) $$ where that last one means \begin{align} v3_x &= v1_x + s(v2_x - v1_x) \\ v3_y &= v1_y + s(v2_y - v1_y) \\ v3_z &= v1_z + s(v2_z - v1_z). \end{align}

Note that if you pick a value $d = 0$, you'll get $v_1$, as expected, and if you pick $d = u$, you'll get $v_2$, as expected. If you pick $d > u$, then you'll get a point beyond $v_2$ on the ray from $v_1$ through $v_2$. And if you pick $d < 0$, you'll get a point on the far side of $v_1$ from $v_2$. In all cases, the points you'll get will be on the line that passes through $v_1$ and $v_2$.

Warning: if $v_1 = v_2$, then you'll get $u = 0$, and dividing by $u$ will fail, so you won't get a good result. Then again, in this case the problem doesn't even make sense, so it's OK.

Related Question