根据左边的距离计算两点之间的当前位置

Calculate current position between two points based on distance left

好的,我在 3d 中有两个位置 space:

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

然后我需要计算当前位置,当someone/something从起点坐标直线移动到终点坐标时。我知道剩下的距离是2,请问当前位置的计算公式是什么?

我想这更像是一道数学题,而不是 javascript 题,但它是针对 javascript 应用程序的,所以我希望这不是问题。

您需要使用 3D 毕达哥拉斯来计算两点之间的距离。如果 x1,y1,z1 和 x2,y2,z2 是您的点,则距离为 sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)。有几种方法可以找到所需的点。我们可以找到从起点到终点的距离,然后使用线性插值计算该距离的比例,结果为 2。

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);

我们得到结果 13.845299461620748 14.845299461620748 15.845299461620748 最后的距离是 2.0000000000000013.

请注意,我使用了 Math.hypot 这是一项新功能,在 Chrome/firefox/opera 中有效,但在 IE 中无效。如果需要,可以在其他浏览器中启用它。您只需使用 Math.sqrt(dx*dx+dy*dy+dz*dz) 即可。

给定两个点,fromPttoPt,可以很容易地计算出两点之间的距离:

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) 

现在沿着直线找到正确的点只是正确插值的一个例子:)

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))  

已经有 2 个算法正确的答案,这个没有什么不同,只是更简洁一些。

// Distance between two points is the square root of the sum
// of the squares of the differences
function get3dDistance(startCoords, endCoords) {
  var dx = Math.pow((startCoords[0] - endCoords[0]), 2);
  var dy = Math.pow((startCoords[1] - endCoords[1]), 2);
  var dz = Math.pow((startCoords[2] - endCoords[2]), 2);
  return Math.sqrt(dx + dy + dz);
}

// The coordinates of a point some distance from the end is
// proportional to the distance left and total distance.
function getCoordsFromDistanceLeft(startCoords, endCoords, distanceLeft) {
  var distance = get3dDistance(startCoords, endCoords);
  var f = (distance - distanceLeft)/distance;
  return [startCoords[0] + f*(endCoords[0] - startCoords[0]),
          startCoords[1] + f*(endCoords[1] - startCoords[1]),
          startCoords[2] + f*(endCoords[2] - startCoords[2])];
}

// Test case
var start = [1,2,3];
var end   = [15,16,17];
var distanceLeft = 2;

// Distance between the two points
var dist = get3dDistance(start, end)

document.write('distance: ' + dist + '<br>');
// distance: 24.24871130596428

// Get the coords
var x = getCoordsFromDistanceLeft(start, end, distanceLeft);

document.write('x: ' + x + ' is ' + distanceLeft + ' to end<br>');
// x: 13.845299461620748,14.845299461620748,15.845299461620748 is 2 to end

document.write('From x to end: ' + get3dDistance(x, end) + '<br>');
// From x to end: 2.0000000000000013

白柳引入了 Math.hypot, which is interesting but since it's a new feature in ECMAScript 2015 it would be wise to include a polyfill