【发布时间】:2021-08-19 19:07:33
【问题描述】:
我只是在研究这个类似蜂窝的结构网络,我是按照这个线程中接受的答案生成的:Hexagonal lattice in different layers by networkx
为方便起见,复制如下:
def node_dist(x,y, cx, cy):
"""Distance of each node from the center of the innermost layer"""
return abs(cx-x) + abs(cy-y)
def remove_unwanted_nodes(G, m):
"""Remove all the nodes that don't belong to an m-layer hexagonal ring."""
#Compute center of all the hexagonal rings as cx, cy
cx, cy = m-0.5, 2*m -(m%2) #odd is 2m-1, even is 2m
#in essence, we are converting from a rectangular grid to a hexagonal ring... based on distance.
unwanted = []
for n in G.nodes:
x,y = n
#keep short distance nodes, add far away nodes to the list called unwanted
if node_dist(x,y, cx, cy) > 2*m:
unwanted.append(n)
#now we are removing the nodes from the Graph
for n in unwanted:
G.remove_node(n)
return G
m = 2 #change m here. 1 = 1 layer, single hexagon.
G = nx.hexagonal_lattice_graph(2*m-1,2*m-1, periodic=False,
with_positions=True,
create_using=None)
pos = nx.get_node_attributes(G, 'pos')
G = remove_unwanted_nodes(G, m)
#render the result
plt.figure(figsize=(4,4))
nx.draw(G, pos=pos, with_labels=True)
plt.axis('scaled')
plt.show()
这会产生这样的情节:
如何找到形成这个蜂窝的所有单独的六边形?
我尝试了循环、派系和锁链,但它们似乎都没有给我想要的答案……除非我做错了什么。
任何帮助将不胜感激!
【问题讨论】:
-
不幸的是,您指定的循环没有任何好的图论属性。您最好的方法可能就是了解您的图表。查看十六进制中最左边的点说 (a, b) 其他点是 (a, b+1), (a, b-1), (a+1, b), (a+1, b+1 ) 和 (a+1, b-1)。