生成三角/六角坐标 (xyz)
我试图提出一个迭代函数来生成六角形网格的 xyz 坐标。使用起始六边形位置(为简单起见,假设为 0,0,0),我想计算每个连续的六边形“环”的坐标,如下所示:
到目前为止,我所能想到的就是这个(javascript 中的示例):
var radius = 3
var xyz = [0,0,0];
// for each ring
for (var i = 0; i < radius; i++) {
var tpRing = i*6;
var tpVect = tpRing/3;
// for each vector of ring
for (var j = 0; j < 3; j++) {
// for each tile in vector
for(var k = 0; k < tpVect; k++) {
xyz[0] = ???;
xyz[1] = ???;
xyz[2] = ???;
console.log(xyz);
}
}
}
我知道每个环比前一个环多包含 6 个点,并且每个 120° 向量对于从中心。我还知道所有图块的 x + y + z = 0
。但是如何生成遵循以下顺序的坐标列表呢?
0, 0, 0
0,-1, 1
1,-1, 0
1, 0,-1
0, 1,-1
-1, 1, 0
-1, 0, 1
0,-2, 2
1,-2, 1
2,-2, 0
2,-1,-1
2, 0,-2
1, 1,-2
0, 2,-2
-1, 2,-1
-2, 2, 0
-2, 1, 1
-2, 0, 2
-1,-1, 2
I'm trying to come up with an iterative function that generates xyz coordinates for a hexagonal grid. With a starting hex position (say 0,0,0 for simplicity), I want to calculate the coordinates for each successive "ring" of hexagons, as illustrated here:
So far, all I've managed to come up with is this (example in javascript):
var radius = 3
var xyz = [0,0,0];
// for each ring
for (var i = 0; i < radius; i++) {
var tpRing = i*6;
var tpVect = tpRing/3;
// for each vector of ring
for (var j = 0; j < 3; j++) {
// for each tile in vector
for(var k = 0; k < tpVect; k++) {
xyz[0] = ???;
xyz[1] = ???;
xyz[2] = ???;
console.log(xyz);
}
}
}
I know each ring contains six more points than the previous and each 120° vector contains one additional point for each step from the center. I also know that x + y + z = 0
for all tiles. But how can I generate a list of coordinates that follow the sequence below?
0, 0, 0
0,-1, 1
1,-1, 0
1, 0,-1
0, 1,-1
-1, 1, 0
-1, 0, 1
0,-2, 2
1,-2, 1
2,-2, 0
2,-1,-1
2, 0,-2
1, 1,-2
0, 2,-2
-1, 2,-1
-2, 2, 0
-2, 1, 1
-2, 0, 2
-1,-1, 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个有趣的谜题。
O(radius2),但(希望)比 Ofri 的风格更多一些 我突然想到,可以像使用方向(移动)向量绕环“行走”一样生成坐标,并且转动相当于围绕移动向量移动零。
此版本还比 Eric 的解决方案 具有优势,因为它从不触及无效坐标(Eric 的解决方案拒绝它们,但这个解决方案从不涉及无效坐标)甚至必须测试它们)。
为 python 的序列切片欢呼三声。
This was a fun puzzle.
O(radius2) but with (hopefully) a bit more style than Ofri's solution. It occurred to me that coordinates could be generated as though you were "walking" around the ring using a direction (move) vector, and that a turn was equivalent to shifting the zero around the move vector.
This version also has the advantage over Eric's solution in that it never touches on invalid coordinates (Eric's rejects them, but this one never even has to test them).
Three cheers for python's sequence slicing.
在尝试了这两个选项之后,我选择了 Ofri 的解决方案,因为它速度稍快一些,并且可以轻松提供初始偏移值。我的代码现在如下所示:
placeTile
方法使用cloneNode
复制预定义的 SVG 元素,每个图块的执行时间大约为 0.5 毫秒,这已经足够好了。非常感谢 tehMick 和 Ofri 的帮助!After trying both the options, I've settled on Ofri's solution as it is a tiny bit faster and made it easy to provide an initial offset value. My code now looks like this:
The
placeTile
method usescloneNode
to copy a predefined SVG element, and it takes approx 0.5ms per tile to execute which is more than good enough. A big thanks to tehMick and Ofri for your help!不仅是
x + y + z = 0
,而且x、y和z的绝对值都等于环半径的两倍。这应该足以识别每个连续环上的每个六边形:Not only is
x + y + z = 0
, but the absolute values of x, y and z are equal to twice the radius of the ring. This should be sufficient to identify every hexagon on each successive ring:另一种可能的解决方案,运行时间为 O(radius2),与 O(radius4) 不同a href="https://stackoverflow.com/a/2049274/241211">tehMick 的解决方案(以牺牲大量风格为代价)是这样的:
或者写得更简洁一点:
它的灵感来自于事实六边形实际上位于六边形本身的外部,因此您可以找到其中 1 个点的坐标,然后通过移动其 6 条边来计算其他点的坐标。
Another possible solution, that runs in O(radius2), unlike the O(radius4) of tehMick's solution (at the expense of a lot of style) is this:
or written a little more concisely:
It's inspired by the fact the hexagons are actually on the exterior of a hexagon themselves, so you can find the coordinates of 1 of its points, and then calculate the others by moving on its 6 edges.