来自六边形顶点的方形XY网格六边形标签

2024-04-19 11:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中生成了一个六边形顶点的xy坐标数组。我想给xy网格添加与它所属六边形相对应的标签。{看起来像是^绘制的顶点

我想要以下格式的文本文件: x y六角

我是Python新手,希望能得到任何帮助。谢谢!


Tags: 网格格式绘制标签数组xy文本文件顶点
1条回答
网友
1楼 · 发布于 2024-04-19 11:27:26

最简单的方法是计算六边形的质心。因此,我没有生成多边形顶点,而是使用以下函数生成多边形质心:calc_polycentroid(startx,starty,endx,endy,a),其中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`

然后定义了x-y网格,并使用cKDTree预测最近邻点作为它最接近的质心的索引。在

^{pr2}$

因此,如果我绘制等高线图,我得到以下结构:N=128 Hexagonal lattice

相关问题 更多 >