选择六边形区域中的邻居细胞

发布于 2024-08-14 05:30:01 字数 849 浏览 7 评论 0原文

想象一下具有 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);

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

Imagine hexagonal space with 3 dimensions.

Each tile has coordinates XYZ.
I need to select a given cell neighbors in the same plane.
With SQL it's looks like:

$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);

But, i don't like this way.
Perhaps someone know more optimal algorithms?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贵在坚持 2024-08-21 05:30:01

这看起来您可以在之间使用

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

This looks like you can use a between

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

This might not exactly work for the $tbDir section. I will have a look at this case in more detail.

OK, rather try this

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)

or even

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
最单纯的乌龟 2024-08-21 05:30:01

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

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

如果您可以接受倾斜的 Y 轴,那么您可以给出 a, d, g X 坐标 0(即 Y 轴穿过这些图块的中心)。 (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 位置。

There is an easy mapping if your algorithms can work with a non-orthogonal coordinate system. In your case, the part of the hex tile which is parallel to an axis seems to be vertical:

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

If you can accept a skew Y axis, then you can give a, d, g the X coordinate 0 (i.e. the Y axis goes through the centers of these tiles). (beh would have X == 1, cfi has X == 2 and so on). x has the coordinate (-1,2). Now you can move like this:

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

As you can see, the movements are now completely independent of the y position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文