好吧,我有在三維空間中的兩個位置:計算當前位置留給
var fromX = 1,
fromY = 2,
fromZ = 3,
toX = 15,
toY = 16,
toZ = 17;
然後我需要計算的當前位置,當某人/某事在一條直線上移動從從座標到座標。我知道剩下的距離是2,計算當前位置的公式是什麼?
我想這是一個比javascript問題更多的數學問題,但它是一個JavaScript應用程序,所以我希望這不是一個問題。
好吧,我有在三維空間中的兩個位置:計算當前位置留給
var fromX = 1,
fromY = 2,
fromZ = 3,
toX = 15,
toY = 16,
toZ = 17;
然後我需要計算的當前位置,當某人/某事在一條直線上移動從從座標到座標。我知道剩下的距離是2,計算當前位置的公式是什麼?
我想這是一個比javascript問題更多的數學問題,但它是一個JavaScript應用程序,所以我希望這不是一個問題。
已經有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,這是有趣的,但因爲它是ECMAScript中2015年的一項新功能,將是明智的,包括polyfill。
我測試了另外兩個答案,就像你說的,他們工作並解決問題。這個例子更多的是爲實際實現做準備,所以我把它作爲問題的答案來檢查。謝謝大家。 – henit
您需要使用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)
。
考慮兩點,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))
您的解決方案有效,請參閱RobG解決方案中的評論以選擇答案。謝謝。 – henit
你真的想要計算當前位置還是中間位置? –
*正在移動* ?!你是什麼意思?我們如何獲得這個職位?步驟是什麼?它是如何移動的? –
上下文是移動,但這對於答案並不重要。我正在尋找的是如何計算從一個點到另一個向量的位置,知道距終點的距離。 – henit