0
我有一个python生成的六角形顶点的xy坐标数组。我想标记对应于它所属六边形的xy网格。当绘制顶点的样子:Vertices正方形XY网格Hexagon Vertices的六角形标签
我要在以下格式的文本文件: X Y六角#
我是新来的Python和希望得到任何帮助。谢谢!
我有一个python生成的六角形顶点的xy坐标数组。我想标记对应于它所属六边形的xy网格。当绘制顶点的样子:Vertices正方形XY网格Hexagon Vertices的六角形标签
我要在以下格式的文本文件: X Y六角#
我是新来的Python和希望得到任何帮助。谢谢!
最简单的方法是计算六边形的质心。因此,而不是产生多边形顶点,我使用下面的函数产生的多边形的质心:calc_polycentroids(startx的,starty,endx,恩迪,a),其中由质心
def calc_polycentroids(startx, starty, endx, endy, a):
# calculate coordinates of the hexagon points
hx=3.*a
hy=a*np.sqrt(3)
origx = startx
origy = starty
# offsets for moving along and up rows
xoffset = hx
yoffset = hy
polygons = []
counter = 0
while starty < endy:
startx = origx
while startx < endx:
p1x = startx + 0.5*a
p1y = starty
p2x = startx + 1.5*a
p2y = starty
p3x = startx + 2.5*a
p3y = starty
p4x = startx
p4y = starty + (a*np.sqrt(3)/2)
p5x = startx + a
p5y = starty + (a*np.sqrt(3)/2)
p6x = startx + 2.*a
p6y = starty + (a*np.sqrt(3)/2)
p7x = startx + 3.*a
p7y = starty + (a*np.sqrt(3)/2)
p8x = startx + 0.5*a
p8y = starty + (a*np.sqrt(3))
p9x = startx + 1.5*a
p9y = starty + (a*np.sqrt(3))
p10x= startx + 2.5*a
p10y= starty + (a*np.sqrt(3))
poly = [
(p1x, p1y),
(p2x, p2y),
(p3x, p3y),
(p4x, p4y),
(p5x, p5y),
(p6x, p6y),
(p7x, p7y),
(p8x, p8y),
(p9x, p9y),
(p10x,p10y),
]
polygons.append(poly)
counter += 1
startx += hx
starty += yoffset
#Truncate points
temp1=np.array(polygons)
temp3=temp1.reshape(temp1.shape[1]*temp1.shape[0],temp1.shape[2])
return temp3`
形成十六进制的=侧然后,定义了在xy网格并使用cKDTree预测最近的邻居作为它最接近的质心的索引。
polygons1=calc_polycentroids(0,0,N,N,a)
x=np.linspace(0,N,N+2);
y=np.linspace(0,N,N+2);
points=np.array(np.meshgrid(x,y))
test_points=np.reshape(np.array([np.ravel(points,order='F')]),((N+2)*(N+2),2))#The x-y points to iterate over
voronoi_kdtree = cKDTree(polygons1) #giving centroids of the grains
test_point_dist, test_point_regions = voronoi_kdtree.query(test_points, k=1)
#test_point_regions Now holds an array of shape (n_test, 1)
#with the indices of the points in polygons1 closest to each of your test_points.
因此,如果我绘制的等高线图,我得到的结构如下:具有N = 128 Hexagonal lattice
现在我有质心的列表和对应的Voronoi图。我想用它的voronoi区域来标记每个(x,y)。 –