开发者

How to store vertices positions of a hexgrid in a 2D Array?

Im facing t开发者_如何学Gohis issue. I want to create an hexgrid and be able to create in this fashion:

//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
 } 
}

All methods I found to make hexgrid, first create an hex object and then replicate it over a grid thus creating duplicate verteces position ad joining edges. I want to avoid duplicating verteces position. How can I declare statements to make such a grid?

Thanks


Let L be length of hexagon side, and let index vertices in column i and row `j in this way:

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

and let (x,y) be coordinate of vertex A (top-left).

Than y coordinate of each row is moved for L*sqrt(3)/2. X coordinate is quite easy to calculate if we look points in hexagon on distance L/4 in x direction from vertices. These points (marked with dots) make lattice with distance L*3/2 in X direction.

Than:

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

The indices of the vertices in one hexagon are of type: (i,j), (i+1,j), (i+1,j+1), (i+1,j+2), (i,j+2), (i,j+1).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜