2016-09-05 2 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) 

코딩 언어는 중요하지 않다 , NON 수학적 형식으로 솔루션을 찾고 있음을 명심하십시오.

업데이트 : 표준 솔루션은 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- 2 점) –

답변

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

과 완벽하게 작동합니다. 타이 –

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

loca = {x = 1474, y = 95, z = 838} 및 locB = {x = 2226, y = 95 인 경우 x, y, z가 질문과 유사하지만 , z = 1190}. 비 수학 수학은 √x, iφ, α 등이 없습니다. –

+1

@RichardAvalos, 이것은 어떤 코디 정치인에게나 적용됩니다. 이 솔루션을 사용하여 거리를 계산할 수 있습니다. http://www.calculatorsoup.com/calculators/geometry-solids/distance-two-points.php –

+1

@RichardAvalos이 좌표는 –