2016-03-23 19 views
0

Oluşturduğum pythonda bir altıgen köşelerin xy koordinatları dizim var. Ait olduğu altıgene karşılık gelen xy ızgarasını etiketlemek istiyorum. x y Altıgen #Kare XY ızgarası Altıgen işaretli altıgen etiketler

Python için yeni ve herhangi bir yardım takdir ediyorum: Ben aşağıdaki biçimde bir metin dosyası istiyorum Vertices

: çizilen zaman olduğu gibi, köşe bak. Teşekkürler!

+0

Şimdi ağırlık merkezlerinin bir listesini ve karşılık gelen Voronoi diyagram sahip olan. Her birini (x, y) voronoi bölgesiyle etiketlemek istiyorum. –

cevap

0

Bunu yapmanın en kolay yolu altıgenlerin merkezlerini hesaplamaktır. Bunun yerine çokgen köşe oluşturma, aşağıdaki fonksiyonu kullanarak poligon ağırlık merkezlerinin oluşturulur: calc_polycentroids (startx, starty, EndX EndY a) ağırlık merkezlerinin

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` 

oluşturduğu heks a = tarafı Sonra xy ızgara tanımlanmıştır burada ve en yakın komşuluğu en yakın merkezin indeksi olarak tahmin etmek için cKDTree kullanmışlardır. Bir dış hat planını arsa halinde

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. 

Böylece, aşağıdaki yapıya elde N = 128 Hexagonal lattice

İlgili konular