蜂窝网格算法

平行状态蜂窝网格

根据网格上来的代码,修改成自己想要的坐标系。


#define CELL_BORDER (40) //六边形边长


#define GRID_WIDTH (CELL_BORDER*1.5f)
#define GRID_HEIGHT (CELL_BORDER*0.8660254f)    // sqr(3)/2=0.8660254
#define TEMP_1 ((GRID_WIDTH*GRID_WIDTH - GRID_HEIGHT*GRID_HEIGHT)/2.f)
#define TEMP_2 ((GRID_WIDTH*GRID_WIDTH + GRID_HEIGHT*GRID_HEIGHT)/2.f)


//2d坐标转网格坐标
void GetCell(int xPos, int yPos, int& cell_x, int& cell_y)
{
cell_x = (int)(xPos / GRID_WIDTH);
float x = xPos - cell_x*GRID_WIDTH;
cell_y = (int)(yPos / (GRID_HEIGHT));
float y = yPos - cell_y*(GRID_HEIGHT);


if((cell_x + cell_y) & 1)
{//奇数
if(x*GRID_WIDTH - y*GRID_HEIGHT > TEMP_1)
cell_x++;
}
else
{
if(x*GRID_WIDTH + y*GRID_HEIGHT > TEMP_2)
cell_x++;
}


cell_y = (int)((cell_y + (1 - (cell_x & 1))) / 2);
}


//网格坐标转2d坐标中心点
void GetCenterPoint(int cell_x, int cell_y, int &xPos, int &yPos)
{
yPos = cell_y * GRID_HEIGHT * 2 + GRID_HEIGHT* (cell_x & 1);
xPos = cell_x * GRID_WIDTH;
}


//2d坐标中心点取六边形的六个端点
void GetCellPoints(int cellx, int celly, int a, POINT *lpPoints)
{
if(lpPoints == NULL) return;
//平
lpPoints[0].x = cellx - a;
lpPoints[0].y = celly;


lpPoints[1].x = cellx - (int)(a * 0.5);
lpPoints[1].y = celly - GRID_HEIGHT;


lpPoints[2].x = cellx + (int)(a * 0.5);
lpPoints[2].y = lpPoints[1].y;


lpPoints[3].x = cellx + a;
lpPoints[3].y = celly;


lpPoints[4].x = lpPoints[2].x;
lpPoints[4].y = celly + GRID_HEIGHT;


lpPoints[5].x = lpPoints[1].x;
lpPoints[5].y = lpPoints[4].y;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值