2

想象一下具有 3 个维度的六边形空间。

每个图块都有坐标 XYZ。我需要在同一平面上选择给定的单元格邻居。使用 SQL,它看起来像:

$tbDir = $y % 2 == 0 ? -1 : 1;

$result =  db_query('SELECT x,y,z FROM {cells} WHERE
                    x = %d AND y = %d AND z = %d OR
                    x = %d AND y = %d AND z = %d OR
                    x = %d AND y = %d AND z = %d OR
                    x = %d AND y = %d AND z = %d OR
                    x = %d AND y = %d AND z = %d OR
                    x = %d AND y = %d AND z = %d OR
                    x = %d AND y = %d AND z = %d ',
                    $x, $y, $z,
                    $x-1, $y, $z,
                    $x+1, $y, $z,
                    $x, $y-1, $z,
                    $x, $y+1, $z,
                    $x+$tbDir, $y-1, $z,
                    $x+$tbDir, $y+1, $z);

但是,我不喜欢这种方式。也许有人知道更优化的算法?谢谢!

4

2 回答 2

4

这看起来像你可以使用之间

x BETWEEN $x-1 AND $x+1 AND y BETWEEN $y-1 AND $y+1 AND z = $z

这可能不适用于 $tbDir 部分。我将更详细地看一下这个案例。

好的,不如试试这个

WHERE   x BETWEEN ($x-1 AND $x+1 AND y = $y AND z = $z)
OR      (y BETWEEN $y-1 AND $y+1 AND x = $x AND z = $z)
OR      (y BETWEEN $y-1 AND $y+1 AND x = $x + $tbDir AND z = $z)

甚至

WHERE   (   (x BETWEEN $x-1 AND $x+1 AND y = $y )
            OR      (y BETWEEN $y-1 AND $y+1 AND x = $x)
            OR      (y BETWEEN $y-1 AND $y+1 AND x = $x + $tbDir)
        )
AND     z = $z
于 2009-12-07T13:40:15.440 回答
3

如果您的算法可以使用非正交坐标系,则映射很容易。在您的情况下,与轴平行的六角瓷砖部分似乎是垂直的:

 / \ / \ / \
| a | b | c |
 \ / \ / \ / \
  | d | e | f |
 / \ / \ / \ /
| x | g | h | i

如果您可以接受倾斜的 Y 轴,那么您可以给aX坐标 0(dgY 轴穿过这些图块的中心)。(beh会有 X == 1,cfi有 X == 2 等等)。x坐标为 (-1,2)。现在你可以像这样移动:

e -> f: x+1,y
e -> d: x-1,y
e -> b: x,  y-1
e -> c: x+1,y-1
e -> g: x-1,y+1
e -> h: x,  y+1

如您所见,运动现在完全独立于 y 位置。

于 2009-12-07T13:51:11.537 回答