Math 如何获取最近自由点的坐标?

Math 如何获取最近自由点的坐标?,math,language-agnostic,Math,Language Agnostic,我有一个空间: [.][.][.][.][.] [.][.][.][.][.] [.][.][x][.][.] [.][.][.][.][.] [.][.][.][.][.] 每个[.]表示二维空间中的一个点。(3D需要它,但现在不重要) 通过[x]我标记了“当前位置”(假设它是[0,0,0]) 所以我需要找到最近的“不忙”点的位置 例如,如果我有这样的区域: [.][.][.][.][.] [.][b][b][b][.] [.][b][x][.][.] [.][b][b][b][.] [.]

我有一个空间:

[.][.][.][.][.]
[.][.][.][.][.]
[.][.][x][.][.]
[.][.][.][.][.]
[.][.][.][.][.]
每个
[.]
表示二维空间中的一个点。(3D需要它,但现在不重要)
通过
[x]
我标记了“当前位置”(假设它是[0,0,0])

所以我需要找到最近的“不忙”点的位置

例如,如果我有这样的区域:

[.][.][.][.][.]
[.][b][b][b][.]
[.][b][x][.][.]
[.][b][b][b][.]
[.][.][.][.][.]
[b]
表示“忙”)

然后我需要得到一个结果(x:0,y:1,z:0)(因为它是最接近的一个自由的)

现在想做这样的事情:

current_location = {x: 0, y: 0, z: 0}
radius = 0 
closest_record = nil
begin
  radius = radius + 1 
  # some way to iterate over each [x, y, z] spot within that radius until
  # closest_record = Record.where(x: x1, y: y1, z: z1).first returns nil (meaning it's free)
end until record.present?
# (that's Ruby syntax, but doesn't matter really)
for(int x=(radius*-1); x<=radius; ++x) {
    for(int y=(radius*-1); y<=radius; ++y) {
        for(int z=(radius*-1); z<=radius; ++z) {
            if(x==0 && y==0 && z==0) {
                continue;
            } else {
                //check location[x,y,z];
            }
        }
    }
}

有什么公式可以做到这一点吗?

你是在问距离公式吗?我不确定我是否理解这个问题,但坐标为(x1,y1,z1)和(x2,y2,z2)的两点之间的距离为:


距离=SqRT((x1-x2)^ 2(y1-y2)^ 2(Z1-Z2)^ 2)

< p>我不熟悉Ruby,但C++中,你可能会做这样的事情:

current_location = {x: 0, y: 0, z: 0}
radius = 0 
closest_record = nil
begin
  radius = radius + 1 
  # some way to iterate over each [x, y, z] spot within that radius until
  # closest_record = Record.where(x: x1, y: y1, z: z1).first returns nil (meaning it's free)
end until record.present?
# (that's Ruby syntax, but doesn't matter really)
for(int x=(radius*-1); x<=radius; ++x) {
    for(int y=(radius*-1); y<=radius; ++y) {
        for(int z=(radius*-1); z<=radius; ++z) {
            if(x==0 && y==0 && z==0) {
                continue;
            } else {
                //check location[x,y,z];
            }
        }
    }
}

for(int x=(radius*-1);xa注意,这将检查半径中的每个点,并从一个角开始。因此,当一个边可能更近时,您可能会得到一个角点结果(取决于您测量距离的方式…具有给定半径的每个元素是否都具有相同的距离?)。此外,如果您检查
radius=1
但未找到任何内容,则将
radius
增加到
2
并使用此结构将检查已检查的
radius=1
中的所有内容。您必须添加一些额外的逻辑以跳过这些检查,否则可能会浪费大量时间。