好的,我在3d空间中有两个位置:
var fromX = 1,
fromY = 2,
fromZ = 3,
toX = 15,
toY = 16,
toZ = 17;然后,我需要计算当前的位置,当某人/某物在从坐标到坐标的直线上移动时。我知道剩下的距离是2,计算当前位置的公式是什么?
我想这更像是一个数学问题,而不是一个javascript问题,但它是一个javascript应用程序,所以我希望这不是一个问题。
发布于 2015-09-04 09:06:47
已经有两个答案与正确的算法,这一个没有什么不同,只是有点整洁。
// 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
Salix已经引入了http://ecma-international.org/ecma-262/6.0/index.html#sec-math.hypot,这是很有趣的,但是由于它是ECMAScript 2015的一个新特性,所以最好包含一个聚脂填充。
发布于 2015-09-04 08:47:29
给定两个点( 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)) 发布于 2015-09-04 08:44:07
你需要使用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)即可。
https://stackoverflow.com/questions/32393024
复制相似问题