• 大小: 20KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-18
  • 语言: C/C++
  • 标签: ASTAR  

资源简介

A*算法的源码好久之前写的了,拿出来稍微整理了一下,看上去一份教科书上的一样标准=。= 没人没有任何技巧性的东西,感觉好悲哀。唯一的亮度就是,功能写好后, 接口封装的还是比较漂亮的~

资源截图

代码片段和文件信息

#include “stdafx.h“
#include “Astar.h“

MAP**  map = NULL;
char** bmap = NULL;

CAstar::~CAstar()
{
}

CAstar::CAstar()
{
nxSize = 0;
nySize = 0;
//方向为左左下下右下左上上右上右;
int tmp[8][2] = { { -1 0 }{ -1 1 }{ 0 1 }{ 1 1 }{ -1 -1 }{ 0 -1 }{ 1 -1 }{ 1 0 } };
int costtemp[8] = { 10 14 10 14 14 10 14 10 };
memcpy(dir tmp sizeof(tmp));
memcpy(cost costtemp sizeof(costtemp));
}

void CAstar::AllocMap(int xSize int ySize)
{
int i;
nxSize = xSize;
nySize = ySize;
bmap = new char*[xSize];
map = new MAP*[xSize];
for (i = 0; i < xSize; i++)
{
bmap[i] = new char[ySize];
map[i] = new MAP[ySize];
}

}
void CAstar::FreeMap()
{
int i;

for ( i = 0; i < nxSize; i++)
{
delete[] bmap[i];
delete[] map[i];
}
delete bmap;
delete map;
}
bool CAstar::ReadBoolMap(char *sMapFile)
{
HANDLE hFile = NULL;
DWORD dwFileSize = 0;
DWORD dwBytes = 0;
void* pMapBuf = 0;
hFile = CreateFileA(sMapFile GENERIC_READ FILE_SHARE_READ NULL OPEN_EXISTING FILE_ATTRIBUTE_NORMAL NULL);
if (INVALID_HANDLE_VALUE== hFile)
{
return false;
}
dwFileSize = GetFileSize(hFile NULL);
if (!dwFileSize)
{
CloseHandle(hFile);
return false;
}
pMapBuf = VirtualAlloc(NULL dwFileSize MEM_COMMITPAGE_EXECUTE_READWRITE);
if (!pMapBuf)
{
CloseHandle(hFile);
return false;
}
if (!ReadFile(hFilepMapBufdwFileSize&dwBytesNULL))
{
CloseHandle(hFile);
return false;
}
CloseHandle(hFile);

for (int i = 0; i < nxSize; i++)
{
CopyMemory(&bmap[i][0] (char*)pMapBuf + i*nySize nySize);
}
VirtualFree(pMapBuf dwFileSize MEM_DECOMMIT);
return true;
}
void CAstar::PrintMap(CWnd* pWnd int bx int by)
{
CBrush brush(RGB(6 255 255));
CClientDC dc(pWnd);
Selectobject(dc brush);
for (int i = bxw=0; i < nxSize; i++w++)
{
for (int j = byh=0; j < nySize; j++h++)
{
if (bmap[i][j] == _map_Type_ok)
{
dc.SetPixel(whRGB(0 0 0));
}
else if(bmap[i][j] == 0)
{
continue;
}
else if (bmap[i][j] == _map_Type_Path)
{
dc.SetPixel(wh RGB(255 0 0));
}
else if (bmap[i][j] == _map_Type_BeginPoint)
{
dc.SetPixel(w h RGB(0 128 255));
}
else if (bmap[i][j] == _map_Type_EndPoint)
{
dc.SetPixel(w h RGB(255 0 0));
}
}

}
ReleaseDC(pWnd->GetSafeHwnd() dc);
}

void CAstar::DirObstacle(int nxint nyint* pob)
{
//方向为左0左下1下2右下3左上4上5右上6右7;
int dx;
int dy;
dx = nx + dir[0][0];
dy = ny + dir[0][1];
if (IsValidPos(dx dy))
{
if (map[dx][dy].type == _map_Type_Obstacle)
{
pob[1] = 0;
pob[4] = 0;
}
}

dx = nx + dir[2][0];
dy = ny + dir[2][1];
if (IsValidPos(dx dy))
{
if (map[dx][dy].type == _map_Type_Obstacle)
{
pob[1] = 0;
pob[3] = 0;
}
}

dx = nx + dir[5][0];
dy = ny + dir[5][1];
if (IsValidPos(dx dy))
{
if (map[dx][dy].type == _map_Type_Obstacle)
{

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件    1100000  2016-04-26 23:29  AStar\2DE584D.NLS1

     文件       7776  2016-08-19 23:36  AStar\Astar.cpp

     文件       4260  2016-08-19 23:36  AStar\Astar.h

     目录          0  2016-08-19 23:38  AStar

----------- ---------  ---------- -----  ----

              1112036                    4


评论

共有 条评论