所以我正在为Hex Board游戏开发GUI。 Hex游戏在11乘11菱形中有121个十六进制形状。我画了一个11×11的板,形状像四边形或正方形。我如何翻译坐标以绘制像菱形一样的十六进制形状?这是我的代码片段,每帧都绘制板。
void draw_Board(HexBoard* h) {
glPushMatrix();
glTranslatef(1.5f, 1.5f, 0.0f);
for (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();
}
以下是我的电路板的样子 http://imgur.com/criFeDM
目标形状就是这样 http://upload.wikimedia.org/wikipedia/commons/3/38/Hex-board-11x11-%282%29.jpg
答案 0 :(得分:2)
您需要掌握六边形背后的基本三角法
如您所见,有两个有效角度30
和60
度。我假设您没有获得六边形点本身的问题,因此请关注网格中的i,j
步骤:
i-step
为2*dx
,x轴为2.0*r*cos(30deg)
j-step
更改展位x和y,它是dx
,y轴为2.0*dx*sin(60deg)
现在只需通过网格创建循环并计算每个六边形起点...... 这是10x10网格的结果:
这里是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; // just shift x,y to start position (i=0,j=0)
x-=float(nj-1)*hexagon_dx*0.5;
y-=float(nj-1)*hexagon_gy*0.5;
for (x0=x,j=0;j<nj;j++,x0+=hexagon_dx,x=x0,y+=hexagon_gy)
for (i=0;i<ni;i++,x+=hexagon_gx)
draw_hexagon(x,y,z);
}
//---------------------------------------------------------------------------
用法很简单:draw_hexagon_grid(0.0,0.0,-7.0,10,10);
x,y,z
是网格的中心点(我有 3D 视图,因此我得到z=-7
只是为了获取相机之前的网格)ni,nj
是x
和y
轴您可以忽略z
轴...