0

スペースがあります:

[.][.][.][.][.]
[.][.][.][.][.]
[.][.][x][.][.]
[.][.][.][.][.]
[.][.][.][.][.]

それぞれ[.]が 2D 空間のスポットを表します。(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)

それを行うための公式はありますか?

4

2 に答える 2

0

私は Ruby には詳しくありませんが、C++ では次のようにすることができます。

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];
            }
        }
    }
}
于 2013-11-11T16:52:39.903 に答える
0

距離の公式を求めていますか?質問がよくわかりませんが、座標 (x1,y1,z1) と (x2,y2,z2) を持つ 2 点間の距離は次のとおりです。

距離 = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2)

于 2013-11-11T16:50:23.870 に答える