c++ - OpenGL 2D Hex Board shaped like a Rhombus -
so working on gui hex board game. hex game has 121 hex shapes in 11 11 rhombus. have drawn 11 11 board shaped quad or square. how go translating coordinates draw hex shapes rhombus? here piece of code draws board every frame.
void draw_board(hexboard* h) { glpushmatrix(); gltranslatef(1.5f, 1.5f, 0.0f); (auto iter = h->drawlist.begin(); iter != h->drawlist.end(); ++iter) { pointref t = *iter; colorref c; float scale_factor = h->get_scale_factor(); if (t.player == 0) { c.red = 1; c.green = 1; c.blue = 1; } else if (t.player == 1) { c.red = 1; c.green = 0; c.blue = 0; } else if (t.player == 2) { c.red = 0; c.green = 0; c.blue = 1; } int x_increment = 2; int y_increment = 2; glpushmatrix(); //cout << t.x_pos << " " << t.y_pos << endl; //cout << c.red << " " << c.green << " " << c.blue << endl; gltranslatef(t.x_pos * 2 , t.y_pos * 2, 0); draw_hex(c, scale_factor); glpopmatrix(); } glpopmatrix(); } here how board looks http://imgur.com/crifedm
goal shape http://upload.wikimedia.org/wikipedia/commons/3/38/hex-board-11x11-%282%29.jpg
you need grasp basic trigonometry behind hexagons

as can see there 2 valid angles 30 , 60 degrees. assume have no problem of obtaining hexagon points focus on i,j step in grid then:
i-step2*dx2.0*r*cos(30deg)in x-axisj-stepchanges booth x , y , isdxin x-axis ,2.0*dx*sin(60deg)in y-axis
now create loop through grid , compute each hexagon start point ... result 10x10 grid:

here source in c++:
//--------------------------------------------------------------------------- #include <math.h> const float hexagon_r=0.2; const float hexagon_dx=hexagon_r*cos(30.0*m_pi/180.0); const float hexagon_dy=hexagon_r*sin(30.0*m_pi/180.0); const float hexagon_gx=2.0*hexagon_dx; const float hexagon_gy=2.0*hexagon_dx*sin(60.0*m_pi/180.0); void draw_hexagon(float x,float y,float z) { glbegin(gl_line_loop); glvertex3f(x-hexagon_dx,y-hexagon_dy,z); glvertex3f(x-hexagon_dx,y+hexagon_dy,z); glvertex3f(x ,y+hexagon_r ,z); glvertex3f(x+hexagon_dx,y+hexagon_dy,z); glvertex3f(x+hexagon_dx,y-hexagon_dy,z); glvertex3f(x ,y-hexagon_r ,z); glend(); } //--------------------------------------------------------------------------- void draw_hexagon_grid(float x,float y,float z,int ni,int nj) { int i,j; float x0; x-=float(ni-1)*hexagon_gx*0.5; // shift x,y start position (i=0,j=0) x-=float(nj-1)*hexagon_dx*0.5; y-=float(nj-1)*hexagon_gy*0.5; (x0=x,j=0;j<nj;j++,x0+=hexagon_dx,x=x0,y+=hexagon_gy) (i=0;i<ni;i++,x+=hexagon_gx) draw_hexagon(x,y,z); } //--------------------------------------------------------------------------- usage simple: draw_hexagon_grid(0.0,0.0,-7.0,10,10);
x,y,zcenter point of grid (i have 3d view therefore gotz=-7grid before camera)ni,njgrid size inx,yaxis
you can ignore z axis ...
Comments
Post a Comment