选择六边形区域中的邻居细胞
想象一下具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来您可以在之间使用
这可能不适用于 $tbDir 部分。我将更详细地研究这个案例。
好吧,不如试试这个
,甚至
This looks like you can use a between
This might not exactly work for the $tbDir section. I will have a look at this case in more detail.
OK, rather try this
or even
如果您的算法可以使用非正交坐标系,则映射很容易。在您的情况下,六角形瓷砖与轴平行的部分似乎是垂直的:
如果您可以接受倾斜的 Y 轴,那么您可以给出
a
,d
,g
X 坐标 0(即 Y 轴穿过这些图块的中心)。 (beh
的 X == 1,cfi
的 X == 2 等等)。x
的坐标为 (-1,2)。现在您可以像这样移动:如您所见,移动现在完全独立于 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:
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:As you can see, the movements are now completely independent of the y position.