我正在尝试提出一个迭代函数,为六边形网格生成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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道每个环包含比前一个多6个点,每个120°向量包含从中心开始的每个步骤的一个附加点.我也知道x + y …
我想画一个带有六角形网格的图形。最终结果应该看起来像蜂窝。但是,我无法使用 matplotlib.collections.RegularPolyCollection 正确调整六边形的大小。任何人都可以看到我做错了什么,或者提供另一种解决方案。我想这以前已经做过了,所以我不需要重新发明轮子。
import matplotlib.pyplot as plt
from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
import numpy as np
# Make some offsets, doing 4 polygons for simplicity here
xyo = [(0,0), (1,0), (0,1), (1,1)]
# length of hexagon side
hexside = 1
# area of circle circumscribing the hexagon
circ_area = np.pi * hexside ** 2
fig, ax = plt.subplots(1,1)
col = collections.RegularPolyCollection(6, np.radians(90), sizes = (circ_area,),
offsets=xyo,transOffset=ax.transData)
ax.add_collection(col, autolim=True)
colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c')]
col.set_color(colors) …Run Code Online (Sandbox Code Playgroud)