?? astar.cpp
字號:
char buf[4];
sprintf(buf,"%d", m_element[i][j]->f);
pDC->TextOut(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,buf);
}
}
}
pDC->SelectObject(poldfont);
newfont->DeleteObject();
*/
if(showMode == 1)
{
//顯示搜索過的點(還在open表里面的是紅色,在close表里面的是綠色的)
for(i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_element[i][j]->f!=-1)
{
if(m_element[i][j]->open)
{
memDC.SelectObject(&bm3);
pDC->BitBlt(j*bmMetric2.bmWidth, i*bmMetric2.bmHeight,bmMetric2.bmWidth,bmMetric2.bmHeight,&memDC,0,0,SRCCOPY);
}
else if(m_element[i][j]->close)
{
memDC.SelectObject(&bm4);
pDC->BitBlt(j*bmMetric2.bmWidth, i*bmMetric2.bmHeight,bmMetric2.bmWidth,bmMetric2.bmHeight,&memDC,0,0,SRCCOPY);
}
//char buf[4];
//sprintf(buf,"%d", m_element[i][j]->f);
//pDC->TextOut(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,buf);
}
}
}
}
//用漸變顏色顯示搜索過的點
if(showMode == 2)
{
//先找出估值的最大值
double max = 0;
for(i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_element[i][j]->f > max)
{
max = m_element[i][j]->f;
}
}
}
//已經(jīng)找到估值的最大值
const int colorBeg = 70;
int totalStep = (255 - colorBeg) * 5;
int R, G, B, temp;
for(int i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_element[i][j]->f != -1)
{
int curStep = (int)(m_element[i][j]->f/max * totalStep);
if(curStep > (temp = (255 - colorBeg) * 4))
{
R = curStep - temp + colorBeg;
G = colorBeg;
B = 255;
}
else if(curStep > (temp = (255 - colorBeg) * 3))
{
R = colorBeg;
G = 255 - (curStep - temp);
B = 255;
}
else if(curStep > (temp = (255 - colorBeg) * 2))
{
R = colorBeg;
G = 255;
B = curStep - temp + colorBeg;
}
else if(curStep > (temp = (255 - colorBeg)))
{
R = 255 - (curStep - temp);
G = 255;
B = colorBeg;
}
else
{
R = 255;
G = curStep + colorBeg;
B = colorBeg;
}
CRect tempRect(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,(j+1)*bmMetric2.bmWidth,(i+1)*bmMetric2.bmHeight);
CBrush brushBg;
brushBg.CreateSolidBrush(RGB(R,G,B));
pDC->FillRect(&tempRect,&brushBg);
}
}
}
}
//結(jié)束描繪漸變色
//用漸變顏色一步一步顯示搜索過的點
if(showMode == 3)
{
//先找出估值的最大值
double max = 0;
for(i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_element[i][j]->f > max)
{
max = m_element[i][j]->f;
}
}
}
//已經(jīng)找到估值的最大值
const int colorBeg = 70;
int totalStep = (255 - colorBeg) * 5;
int R, G, B, temp;
for(int i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_element[i][j]->f != -1)
{
int curStep = (int)(m_element[i][j]->f/max * totalStep);
if(curStep > (temp = (255 - colorBeg) * 4))
{
R = curStep - temp + colorBeg;
G = colorBeg;
B = 255;
}
else if(curStep > (temp = (255 - colorBeg) * 3))
{
R = colorBeg;
G = 255 - (curStep - temp);
B = 255;
}
else if(curStep > (temp = (255 - colorBeg) * 2))
{
R = colorBeg;
G = 255;
B = curStep - temp + colorBeg;
}
else if(curStep > (temp = (255 - colorBeg)))
{
R = 255 - (curStep - temp);
G = 255;
B = colorBeg;
}
else
{
R = 255;
G = curStep + colorBeg;
B = colorBeg;
}
CRect tempRect(j*bmMetric2.bmWidth,i*bmMetric2.bmHeight,(j+1)*bmMetric2.bmWidth,(i+1)*bmMetric2.bmHeight);
CBrush brushBg;
brushBg.CreateSolidBrush(RGB(R,G,B));
pDC->FillRect(&tempRect,&brushBg);
}
}
}
}
//用漸變顏色一步一步顯示搜索過的點結(jié)束
//顯示最短路徑
if(showMode != 3)
{
CPen pen,*p_pen;
pen.CreatePen(PS_SOLID,2,RGB(0,0,255));
p_pen = pDC->SelectObject(&pen);
pDC->MoveTo(targetX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , targetY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
while(tempHead!=NULL)
{
pDC->LineTo(tempHead->x * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , tempHead->y * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
tempHead = tempHead->parent;
}
pDC->SelectObject(p_pen);
pen.DeleteObject();
}
memDC.SelectObject(pbm);
bm1.DeleteObject();
bm2.DeleteObject();
memDC.DeleteDC();
//return time;//返回搜索路徑所需要的時間
}
void CAStar::prepareForStepByStep()
{
for(int i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(map[i][j]==0)
{
head = m_element[i][j];
head->x=j;
head->y=i;
head->fore=NULL;
head->next=NULL;
head->g=0;
//head->h=
head->close=0;
head->open=1;
//head->f=
head->parent=NULL;
}
else if(map[i][j]==-3)
{
targetX=j;
targetY=i;
}
}
}
}
int CAStar::searchThePathStepByStep()
{
if(head!=NULL)
{
as_node *tempHead;
if(head->next==NULL)
{
tempHead=NULL;
}
else
{
tempHead=head->next;//head下一個節(jié)點作為新的鏈表頭指針
head->next->fore=NULL;
}
m_element[head->y][head->x]->open=0;
m_element[head->y][head->x]->close=1;//把head放進close表
if(map[head->y][head->x+1]!=-1)//如果臨近的方格不是墻壁
{
if(m_element[head->y][head->x+1]->close)
{}
else if(m_element[head->y][head->x+1]->open)
{
int temp = m_element[head->y][head->x]->g+1+m_element[head->y][head->x+1]->h;
if(m_element[head->y][head->x+1]->f > temp)
{
m_element[head->y][head->x+1]->g = m_element[head->y][head->x]->g+1;
m_element[head->y][head->x+1]->f = temp;
m_element[head->y][head->x+1]->parent = m_element[head->y][head->x];
tempHead=adjustNode(tempHead,m_element[head->y][head->x+1]);
}
}
else
{
if(head->x+1==targetX && head->y==targetY){return 1;}//找到終點!//修改這里
//as_node *node=new as_node;
as_node *node=m_element[head->y][head->x+1];
node->x=head->x+1;//修改這里
node->y=head->y;//修改這里
node->open=1;
node->close=0;
node->parent=m_element[head->y][head->x];
node->g=node->parent->g+1;
node->h=abs(node->x - targetX) + abs(node->y - targetY);
node->f=node->g + node->h;
tempHead=insertNode(tempHead,node);
}
//最后記住把head放進close表
}
if(map[head->y][head->x-1]!=-1)//如果臨近的方格不是墻壁
{
if(m_element[head->y][head->x-1]->close)
{}
else if(m_element[head->y][head->x-1]->open)
{
int temp = m_element[head->y][head->x]->g+1+m_element[head->y][head->x-1]->h;
if(m_element[head->y][head->x-1]->f > temp)
{
m_element[head->y][head->x-1]->g = m_element[head->y][head->x]->g+1;
m_element[head->y][head->x-1]->f = temp;
m_element[head->y][head->x-1]->parent=m_element[head->y][head->x];
tempHead=adjustNode(tempHead,m_element[head->y][head->x-1]);
}
}
else
{
if(head->x-1==targetX && head->y==targetY){return 1;}//找到終點!//修改這里
//as_node *node=new as_node;
as_node *node=m_element[head->y][head->x-1];
node->x=head->x-1;//修改這里
node->y=head->y;//修改這里
node->open=1;
node->close=0;
node->parent=m_element[head->y][head->x];
node->g=node->parent->g+1;
node->h=abs(node->x - targetX) + abs(node->y - targetY);
node->f=node->g + node->h;
tempHead=insertNode(tempHead,node);
}
//最后記住把head放進close表
}
if(map[head->y+1][head->x]!=-1)//如果臨近的方格不是墻壁
{
if(m_element[head->y+1][head->x]->close)
{}
else if(m_element[head->y+1][head->x]->open)
{
int temp = m_element[head->y][head->x]->g+1+m_element[head->y+1][head->x]->h;
if(m_element[head->y+1][head->x]->f > temp)
{
m_element[head->y+1][head->x]->g = m_element[head->y][head->x]->g+1;
m_element[head->y+1][head->x]->f = temp;
m_element[head->y+1][head->x]->parent=m_element[head->y+1][head->x];
tempHead=adjustNode(tempHead,m_element[head->y+1][head->x]);
}
}
else
{
if(head->x==targetX && head->y+1==targetY){return 1;}//找到終點!//修改這里
//as_node *node=new as_node;
as_node *node=m_element[head->y+1][head->x];
node->x=head->x;//修改這里
node->y=head->y+1;//修改這里
node->open=1;
node->close=0;
node->parent=m_element[head->y][head->x];
node->g=node->parent->g+1;
node->h=abs(node->x - targetX) + abs(node->y - targetY);
node->f=node->g + node->h;
tempHead=insertNode(tempHead,node);
}
//最后記住把head放進close表
}
if(map[head->y-1][head->x]!=-1)//如果臨近的方格不是墻壁
{
if(m_element[head->y-1][head->x]->close)
{}
else if(m_element[head->y-1][head->x]->open)
int temp = m_element[head->y][head->x]->g+1+m_element[head->y-1][head->x]->h;
if(m_element[head->y-1][head->x]->f > temp)
{
m_element[head->y-1][head->x]->g = m_element[head->y][head->x]->g+1;
m_element[head->y-1][head->x]->f = temp;
m_element[head->y-1][head->x]->parent=m_element[head->y-1][head->x];
tempHead=adjustNode(tempHead,m_element[head->y-1][head->x]);
}
}
else
{
if(head->x==targetX && head->y-1==targetY){return 1;}//找到終點!//修改這里
//as_node *node=new as_node;
as_node *node=m_element[head->y-1][head->x];
node->x=head->x;//修改這里
node->y=head->y-1;//修改這里
node->open=1;
node->close=0;
node->parent=m_element[head->y][head->x];
node->g=node->parent->g+1;
node->h=abs(node->x - targetX) + abs(node->y - targetY);
node->f=node->g + node->h;
tempHead=insertNode(tempHead,node);
}
//最后記住把head放進close表
}
head=tempHead;
return 2;
}
return 0;//如果head是NULL表示open表已為空,所以找不到路徑
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -