2016-03-23 52 views
0

我有一個python生成的六角形頂點的xy座標數組。我想標記對應於它所屬六邊形的xy網格。當繪製頂點的樣子:Vertices正方形XY網格Hexagon Vertices的六角形標籤

我要在以下格式的文本文件: X Y六角#

我是新來的Python和希望得到任何幫助。謝謝!

+0

現在我有質心的列表和對應的Voronoi圖。我想用它的voronoi區域來標記每個(x,y)。 –

回答

0

最簡單的方法是計算六邊形的質心。因此,而不是產生多邊形頂點,我使用下面的函數產生的多邊形的質心: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