0
$\begingroup$

I wish to travel in 3 dimensions from the a start to a target position, a given distance.

sx and sy are the x and y coordinates of the start tx and ty are the x and y coordinates of the target rx and ry are the resulting mid way coordinates 'dist' is the distance to travel  dist_between = sqrt((sx - tx) ^ 2 + (sy - ty) ^ 2)  ratio = (dist_between - dist) / dist_between; rx += (tx + ratio * (sx - tx)) - sx; ry += (ty + ratio * (sy - ty)) - sy; 

Will this scale to 3D as follows?

sx, sy and sz are the x, y and z coordinates of the start tx, ty and tz are the x, y and z coordinates of the target rx, ry and rz are the resulting mid way coordinates 'dist' is the distance to travel  dist_between = sqrt((sx - tx) ^ 2 + (sy - ty) ^ 2 + (sz - tz) ^ 2)  ratio = (dist_between - dist) / dist_between; rx = sx + (tx + ratio * (sx - tx)) - sx; ry = sy + (ty + ratio * (sy - ty)) - sy; rz = sz + (tz + ratio * (sz - tz)) - sz; 

2 Answers 2

1

It scales just the way you have suggested. The normal way to write it would be ratio2=dist/dist_between; rx=sx+ratio2*(tx-sx), which has a few less operations than your approach. This ratio2 is your ratio subtracted from 1, so ratio=0 makes you return sx. In your case, ratio=1 makes you return tx. Either works, but it seems more natural to think of ratio2=0 being at the start.

In your 2D example, you use += for the assignment. I believe that adds the right side to whatever is already in rx, which means you will get the wrong answer if rx isn't what you think it is. It looks like you expect rx to be sx on entry. In your 3D example, sx cancels out on the right.

0

In 3D:

the result of the first 2D operation is the cathete for the 3d operation

dist_between $= \sqrt{(sx - tx) ^ 2} + \sqrt{(sy - ty) ^ 2 + (sz - tz) ^ 2)}$

calcoulus of the distance intermediate should be correct.