2011-07-11 24 views
2

我正面臨着這個問題。我想創建一個hexgrid並能夠以這種方式來創建:如何在二維數組中存儲六角網格的頂點位置?

//grid extents 
int numCols,numRows; 
for (int i=0; i<numCols; ++i){ 
for (int j=0; j<numRows; ++j){ 

//x and y coordinates of my hexagon's vertices 
float xpos,ypos; 


//2D array storing verteces of my hextopology 
vertices[i][j] = new VertexClass(xpos, ypos); 

// statements to change xpos/ypos and create hex 
} 
} 

所有方法我發現做hexgrid,首先創建一個十六進制對象,然後將它複製了因此造成重複verteces位置廣告鄰接邊緣網格。我想避免重複verteces的位置。我怎樣才能聲明這樣的網格?

感謝

回答

2

L是六邊形的邊長,讓指數頂點i柱並以這種方式排`記者:

i 0 0 1 1 2 2 3... 
j  \ /  \ /
0 . A---o .  . o---o 
    / \  / \ 
    /  \  /
    /  \ /
1 -o .  . o---o . 
    \  / \ 
    \  /  \ 
     \ /  \ /
2 . o---o .  . o---o 
    / \  / \ 

,讓(x,y)頂點A來協調(頂剩下)。

比y座標的每一行都移動了L*sqrt(3)/2。如果我們在距離頂點的x方向距離L/4上查看六角形中的點,則X座標很容易計算。這些點(用點標記)在X方向上的距離爲L*3/2

比:

vertices[i][j] = Vertex(x - L/4 + i*L*3/2 + L/4*(-1)^(i+j), y - j*L*sqrt(3)/2) 

在一個六角形的頂點的指數類型:(i,j), (i+1,j), (i+1,j+1), (i+1,j+2), (i,j+2), (i,j+1)