2016-03-23 25 views
0

私は、生成したPythonの六角形の頂点のxy座標の配列を持っています。私はそれが属する六角形に対応するxyグリッドにラベルを付ける必要があります。 のx yの六角#正方形のXYグリッド六角形の頂点からの六角ラベル

私は、Pythonに新しいですし、任意の助けをいただければ幸いです:私は、次の形式のテキストファイルをしたいVertices

:プロットするときのように、頂点が見えます。ありがとう!

+0

Nで今は重心のリストと、対応するボロノイ図を有します。私は各(x、y)にそのボロノイ領域をラベルしたいと思います。 –

答えて

0

これを行う最も簡単な方法は、六角形の重心を計算することです。代わりに、ポリゴンの頂点を生成するので、私は、次の関数を使用して、ポリゴンの重心を発生:calc_polycentroids(startxと、startYと、ENDX、ENDYは、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を使用して、それに最も近い重心のインデックスとして最も近い隣を予測します。 Iは、等高線プロットをプロットした場合

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. 

したがって、私は、以下の構造を得る:= 128 Hexagonal lattice

関連する問題