2016-09-05 135 views
0

我試圖找到兩個已經存在的位置(x,y,z)之間的新位置(x,y,z)。找到其他兩個位置之間的位置

例如可以說locA和locB之間的距離是2500. locNew應該始終是距離爲300的位置,並且應該位於locA和locB的行上。

我沒有發現locA和locB之間的中點的問題,但我不停地敲我的頭,試圖找到locNew這個特定的情況。

我想這一點,但它返回一個點,不就行了從LOCA到locB:

locA = {x = 400, y = 400, z = 400} 
locB = {x = 1200, y = 1200, z = 1200} 

--this is wrong somehow 
locNew_x = (locB.x+locA.x)-(locB.x-300) 
locNew_y = (locB.y+locA.y)-(locB.y-300) 
locNew_z = (locB.z+locA.z)-(locB.z-300) 
locNew = {x = locNew_x, y = locNew_y, z = locNew_z} 

--draws a line between two objects 
DrawLine(locA, locNew) 

編碼語言並不重要,因爲計算應該是「幾乎」在大多數語言相同,請記住,即時通訊尋找在一個非數學形式的解決方案。

更新: 標準溶液工作,如果X,Y,Z是相同的,但如果他們都像在下面的例子不同。

locA = {x = 1475, y = 95, z = 838} 
locB = {x = 2226, y = 110, z = 1190} 
+0

的[?查找兩個點之間的點的座標(可能的複製http://stackoverflow.com/questions/2886092/finding-coordinates-of-a-point-between-兩點) –

回答

0

是什麼意思我想這可能會幫助您:

-- Substract vectors 
function subVectors(vector_A, vector_B) 
    return {x = (vector_A.x - vector_B.x), 
      y = (vector_A.y - vector_B.y), 
      z = (vector_A.z - vector_B.z)} 
end 

--- Calculate length of vector 
function vectorLength(vector_A) 
    return math.sqrt(
     (vector_A.x * vector_A.x) + 
     (vector_A.y * vector_A.y) + 
     (vector_A.z * vector_A.z) 
    ) 
end 

-- Convert to unit vector 
function toUnitVector(vector_A) 
    local ln = vectorLength(vector_A) 
    return {x = (vector_A.x/ln), y = (vector_A.y/ln), z = (vector_A.z/ln)} 
end 

-- calculate position of vector which is on the line between A and B and 
-- its distance from B point equals `distance` 
function distancedVector(vector_A, vector_target, distance) 
    local vec = subVectors(vector_A, vector_target) 
    local unitVec = toUnitVector(vec) 

    return { 
     x = (vector_target.x + unitVec.x * distance), 
     y = (vector_target.y + unitVec.y * distance), 
     z = (vector_target.z + unitVec.z * distance) 
    } 
end 

local locA = {x = 0.0, y = 0.0, z = 0.0} 
local locB = {x = 900.0, y = 900.0, z = 900.0} 

local ret = distancedVector(locA, locB, 10) 

print(string.format("x: %f\ny: %f\nz: %f\n", ret.x, ret.y, ret.z)) 

輸出:

x: 894.226497 
y: 894.226497 
z: 894.226497 

相關:Move point to another in c#

+0

完美的作品! TY –

1

我認爲這應該工作:

locA = {x = 400, y = 400, z = 400} 
locB = {x = 1200, y = 1200, z = 1200} 

scalar = 300/distance(locA,locB); --target distance/existing distance 

locNew_x = locA.x + (locB.x - locA.x) * scalar 
locNew_y = locA.y + (locB.y - locA.y) * scalar 
locNew_z = locA.z + (locB.z - locA.z) * scalar 
locNew = {x = locNew_x, y = locNew_y, z = locNew_z} 


DrawLine(locA, locNew) 

很抱歉,如果這不回答你的問題,我不是很確定你所說的「非數學形式」

+0

如果x,y,z與問題中相同,但當locA = {x = 1474,y = 95,z = 838}且locB = {x = 2226,y = 95 ,Z = 1190}。非數學是沒有√x,iφ,α..等 –

+1

@RichardAvalos,這適用於任何co-ordiantes。你可以使用這個解決方案來計算距離http://www.calculatorsoup.com/calculators/geometry-solids/distance-two-points.php –

+1

@RichardAvalos這應該適用於任何座標 –

相關問題