Grid 如何在二维阵列中存储六角网格的顶点位置?

Grid 如何在二维阵列中存储六角网格的顶点位置?,grid,hex,hexagonal-tiles,Grid,Hex,Hexagonal Tiles,我面临这个问题。我想创建一个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 hextopol

我面临这个问题。我想创建一个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
 } 
}
//网格范围
int numCols,numRows;

对于(int i=0;i让
L
为六边形边的长度,并让列
i
和行'j中的索引顶点按如下方式:

 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
的晶格

比:

一个六边形中顶点的索引类型为:
(i,j)、(i+1,j)、(i+1,j+1)、(i+1,j+2)、(i,j+2)、(i,j+1)

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