Question: Ok, I have two positions in 3d space:

```
var fromX = 1,
    fromY = 2,
    fromZ = 3,
    toX = 15,
    toY = 16,
    toZ = 17;

```

Then I need to calculate the current position, when someone/something is moving in a straight line from the from-coordinates, to the to-coordinates. I know the distance left is 2, what would be the formula for calculating the current position?

I guess this is more of a math question than a javascript question, but it is for a javascript application, so I'm hoping that is not a problem.

Answer: 
Given two points, `fromPt` and `toPt`, the distance between two points can easily be calculated: 

```
distanceX = Math.pow(fromPt.x - toPt.x, 2)
distanceY = Math.pow(fromPt.y - toPt.y, 2)
distanceZ = Math.pow(fromPt.z - toPt.z, 2)
total_distance = Math.sqrt(distanceX + distanceY + distanceZ) 

```

and now finding the correct point along the line is just a case of correct interpolation :) 

```
newPt = {}
newPt.x = fromPt.x + ((toPt.x - fromPt.x) * (wantedDistance / total_distance))
newPt.y = fromPt.y + ((toPt.y - fromPt.y) * (wantedDistance / total_distance))
newPt.z = fromPt.z + ((toPt.z - fromPt.z) * (wantedDistance / total_distance))  

```
You need to use 3D Pythagoras to find the distance between two points. If x1,y1,z1 and x2,y2,z2 are your points then the distance is `sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)`. There are several ways of finding the desired point. We can find the distance from the starting point to the ending point and then calculate the proportion of that distance which will give 2 as a result using linear interpolation.

```
var fromX = 1,
  fromY = 2,
  fromZ = 3,
  toX = 15,
  toY = 16,
  toZ = 17;
// find the difference
var dx = toX-fromX, dy = toY-fromY, dz=toZ-fromZ;
// find the total length
var dist = Math.hypot(dx,dy,dz);

// find the proportion of this length
var lambda = (dist-2.0) / dist;

// do the linear interpolation
var x = fromX + lambda * dx,
    y = fromY + lambda * dy,
    z = fromZ + lambda * dz;
console.log(x,y,z);

// Just to check
var dx2 = toX-x, dy2 = toY-y, dz2=toZ-z;
var dist2 = Math.hypot(dx2,dy2,dz2);
console.log(dist2);

```

We get the result 13.845299461620748 14.845299461620748 15.845299461620748 and the final distance is 2.0000000000000013.

Note I've uses [Math.hypot](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot) this is a new feature which works in Chrome/firefox/opera but not in IE. There is a work-around to enable it in other browsers if needed. You just use `Math.sqrt(dx*dx+dy*dy+dz*dz)` instead.