想象一下具有 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);
但是,我不喜欢这种方式。也许有人知道更优化的算法?谢谢!