【发布时间】:2020-02-16 11:09:38
【问题描述】:
我正在尝试使用立方坐标存储六边形图,详见https://www.redblobgames.com/grids/hexagons/#coordinates
我想使用向量或一些易于迭代的容器来存储 Hex 对象,基本上是 3D 整数坐标的集合加上包含相邻 Hex 索引的向量。
我的问题是我无法想出将立方 X/Y/Z 坐标(允许负整数值)转换为相应的 1D 容器索引的函数。给定任何有效的坐标集,我希望能够获得十六进制的特定索引号。
我知道有一个类似的功能
1D_index = f(x,y,z) = x + (max_width)*y + (max_width)(max_height)*z,
对于笛卡尔网格,但我无法在六边形空间中生成类似的函数:
我的 Hex 类和容器是这样的:
class Hex
{
private:
int xCell, yCell, zCell;
std::vector<int>neighbours;
public:
Hex();
Hex(int x, int y, int z) : _x(x), _y(y), _z(z);
~Hex();
};
std::vector<Hex*>hexGraph;
为了用未连接的节点填充图形,我只列出了所有可能存在的坐标,给定图形的正负 x/y/z 维度:
for(int x = effectiveWorldNegX; x < effectiveWorldPosX; x++)
for(int y = effectiveWorldPosY; y > effectiveWorldNegY; y--)
hexGraph.push_back(new Hex(x,y,(x*-1)-y)); // x+y+z=0, so z=(-x-y)
现在我被难住了。我想使用这样的循环连接节点:
for(std::vector<Hex*>iterator it = hexGraph.begin(); it != hexGraph.end(); ++it)
{
(*it)->neighbours.push_back( /* Index of upper left hex node */ )
(*it)->neighbours.push_back( /* Index of upper center hex node */ )
(*it)->neighbours.push_back( /* Index of upper right hex node */ )
...
}
等等,但为了做到这一点,我需要一种方法来找到任何单元格 +1x、-1y、+0z 的索引。
从上面的循环中,我确定的是 1) 给定循环的开始/结束,将 Hex 推回 hexGraph 向量的确切顺序
2) 我可能需要也可能不需要将所有坐标值偏移 X/Y/Z 与 0 的负距离以去除负值
但是我可以从这里去哪里?
非常感谢您的帮助。
【问题讨论】:
标签: c++ containers coordinates