好的,我在3d空间中有两个位置:
var fromX = 1,
fromY = 2,
fromZ = 3,
toX = 15,
toY = 16,
toZ = 17;
然后我需要计算当前位置,当某人/某物从直径从坐标移动到坐标时。我知道剩下的距离是2,计算当前位置的公式是什么?
我想这更像是一个数学问题,而不是一个javascript问题,但它适用于javascript应用程序,所以我希望这不是问题。
答案 0 :(得分:1)
您需要使用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)
。
答案 1 :(得分:1)
给定两点,fromPt
和toPt
,可以轻松计算两点之间的距离:
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 :(得分:1)
正确的算法已经有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
&#13;
Salix alba介绍了Math.hypot,这很有意思,但由于它是ECMAScript 2015的一个新功能,因此加入polyfill是明智的。