在其他两个位置之间找到位置 [英] find location between two other locations

查看:117
本文介绍了在其他两个位置之间找到位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个现有位置(x,y,z)之间找到一个新位置(x,y,z)。

I am trying trying to find a new location(x,y,z) between two already existing locations (x,y,z).

例如可以说locA和locB之间的距离是2500。locNew应该始终是距离为300的位置,并且应该位于locA和locB的线上。

e.g. lets say the distance between locA and locB is 2500. locNew should always be the location with distance 300 and should be on the line of locA and locB.

我没有问题找到locA和locB之间的中点,但是我一直在努力寻找适合这种特定情况的locNew。

I have no issues finding the midpoint between locA and locB, but I keep banging my head trying to find locNew for this specific situation.

我尝试过此操作,但是它返回的点不在从locA到locB的线上:

I tried this, but it returns a point that is not on the line from locA to 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)

编码语言并不重要,因为在大多数语言中计算应该看起来几乎是相同的,请记住我正在寻找非数学形式的解决方案。

Coding language is not important, since the calculation should look 'almost' the same in most languages, keep in mind that im looking for a solution in a NON mathematical form.

更新:如果x,y,z相同,则
标准解决方案有效,但如以下示例中所述,
标准解决方案则不同。

Update: standard solutions works if x,y,z is the same, but not if they are different like in the example below.

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


推荐答案

可能会帮助您:

-- 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

相关:将指针移至c#中的另一个

这篇关于在其他两个位置之间找到位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆