2015-09-04 89 views
0

好吧,我有在三维空间中的两个位置:计算当前位置留给

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

然后我需要计算的当前位置,当某人/某事在一条直线上移动从从坐标到坐标。我知道剩下的距离是2,计算当前位置的公式是什么?

我想这是一个比javascript问题更多的数学问题,但它是一个JavaScript应用程序,所以我希望这不是一个问题。

+0

你真的想要计算当前位置还是中间位置? –

+0

*正在移动* ?!你是什​​么意思?我们如何获得这个职位?步骤是什么?它是如何移动的? –

+0

上下文是移动,但这对于答案并不重要。我正在寻找的是如何计算从一个点到另一个向量的位置,知道距终点的距离。 – henit

回答

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

白柳先后引进Math.hypot,这是有趣的,但因为它是ECMAScript中2015年的一项新功能,将是明智的,包括polyfill

+0

我测试了另外两个答案,就像你说的,他们工作并解决问题。这个例子更多的是为实际实现做准备,所以我把它作为问题的答案来检查。谢谢大家。 – henit

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)

+0

值得指出[* Math.hypot *](http://ecma-international.org/ecma-262/6.0/index.html#sec-math.hypot)是ECMAScript 2015的一项功能,所以值得使用polyfill以防万一。 – RobG

+0

您的解决方案有效,请在RobG的解决方案中查看我的评论以选择答案。谢谢。 – henit

1

考虑两点,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)) 
+0

您的解决方案有效,请参阅RobG解决方案中的评论以选择答案。谢谢。 – henit