📄 bothwave.cpp
字号:
{
newP->next = newS;
newP = newS;
}
}
ii = i-1;
if((map[i-1][j]!=-1)&&(m_node[i-1][j].close2==0))//如果不在close列表中
{
if(m_node[ii][j].close1==1)
{
linkY = ii;
linkX = j;
return 1;
}
m_node[ii][j].value = m_node[i][j].value + 1;
m_node[ii][j].close2 = 1;
newS = m_pNode[ii][j];
newS->next=NULL;
if(newHead==NULL)
{
newHead = newS;
newP = newS;
}
else
{
newP->next = newS;
newP = newS;
}
}
jj = j+1;
if((map[i][j+1]!=-1)&&(m_node[i][j+1].close2==0))//如果不在close列表中
{
if(m_node[i][jj].close1==1)
{
linkY = i;
linkX = jj;
return 1;
}
m_node[i][jj].value = m_node[i][j].value + 1;
m_node[i][jj].close2 = 1;
newS = m_pNode[i][jj];
newS->next=NULL;
if(newHead==NULL)
{
newHead = newS;
newP = newS;
}
else
{
newP->next = newS;
newP = newS;
}
}
jj = j-1;
if((map[i][j-1]!=-1)&&(m_node[i][j-1].close2==0))//如果不在close列表中
{
if(m_node[i][jj].close1==1)
{
linkY = i;
linkX = jj;
return 1;
}
m_node[i][jj].value = m_node[i][j].value + 1;
m_node[i][jj].close2 = 1;
newS = m_pNode[i][jj];
newS->next=NULL;
if(newHead==NULL)
{
newHead = newS;
newP = newS;
}
else
{
newP->next = newS;
newP = newS;
}
}
s = s->next;
}//向外扩展一步结束
head2 = newHead;
return 2;
}
return 0;
}
void CBothWave::showThePath(CDC *pDC , int showMode)
{
CBitmap bm1,bm2,bm3,bm5,bm6, *pbm;
BITMAP bmMetric1,bmMetric2;
bm1.LoadBitmap(IDB_BITMAP7);
bm2.LoadBitmap(IDB_BITMAP8);
bm3.LoadBitmap(IDB_BITMAP11);
bm5.LoadBitmap(IDB_BITMAP12);//起点
bm6.LoadBitmap(IDB_BITMAP13);//终点
bm1.GetBitmap(&bmMetric1);
bm2.GetBitmap(&bmMetric2);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
pbm = memDC.SelectObject(&bm1);
if(showMode == 1)
{
//描绘搜索过的点
for(int i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_node[i][j].value>=1)
{
memDC.SelectObject(&bm3);
pDC->BitBlt(j*bmMetric1.bmHeight, i*bmMetric1.bmWidth,bmMetric1.bmWidth,bmMetric1.bmHeight,&memDC,0,0,SRCCOPY);
//char buf[4];
//sprintf(buf,"%d", m_map[i][j]);
//pDC->TextOut(j*bmMetric2.bmWidth+5,i*bmMetric2.bmHeight+5,buf);
}
}
}
//结束描绘搜索过的点
}
//用渐变颜色显示搜索过的点
if(showMode == 2)
{
const int colorBeg = 70;
int totalStep = (255 - colorBeg) * 5;
double max = m_node[linkY][linkX].value;
int R, G, B, temp;
for(int i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_node[i][j].value != -1)
{
int curStep = (int)(m_node[i][j].value/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);
}
}
}
}
//结束描绘渐变色
//用渐变颜色一步一步描绘搜索过的点
if(showMode == 3)
{
double max = 0;
for(int i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_node[i][j].value > max)
{
max = m_node[i][j].value;
}
}
}
const int colorBeg = 70;
int totalStep = (255 - colorBeg) * 5;
//double max = m_node[linkY][linkX].value;
int R, G, B, temp;
for(i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_node[i][j].value != -1)
{
int curStep = (int)(m_node[i][j].value/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);
}
}
}
}
if(showMode != 3)
{
//描绘最短路径
CPen pen,*p_pen;
pen.CreatePen(PS_SOLID,2,RGB(0,0,255));
p_pen = pDC->SelectObject(&pen);
int i,j,min;
pDC->MoveTo(linkX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , linkY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
i=linkY;
j=linkX;
min = m_node[linkY][linkX].value;
if(m_node[linkY][linkX].close1==1)//如果连接点属于起点生成的
{
while(m_node[i][j].value!=0)
{
if(m_node[i+1][j].value == min-1 && m_node[i+1][j].close1==1)
{
i = i+1;
}
else if(m_node[i-1][j].value == min-1 && m_node[i-1][j].close1==1)
{
i = i-1;
}
else if(m_node[i][j+1].value == min-1 && m_node[i][j+1].close1==1)
{
j = j+1;
}
else if(m_node[i][j-1].value == min-1 && m_node[i][j-1].close1==1)
{
j = j-1;
}
min--;
pDC->LineTo(j * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , i * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
}
pDC->LineTo(startX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , startY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
pDC->MoveTo(linkX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , linkY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
i=linkY;
j=linkX;
if(m_node[i+1][j].close2==1)
{
min = m_node[i+1][j].value;
i = i+1;
}
else if(m_node[i-1][j].close2==1)
{
min = m_node[i-1][j].value;
i = i-1;
}
else if(m_node[i][j+1].close2==1)
{
min = m_node[i][j+1].value;
j = j+1;
}
else if(m_node[i][j-1].close2==1)
{
min = m_node[i][j-1].value;
j = j-1;
}
while(m_node[i][j].value!=0)
{
if(m_node[i+1][j].value == min-1 && m_node[i+1][j].close2==1)
{
i = i+1;
}
else if(m_node[i-1][j].value == min-1 && m_node[i-1][j].close2==1)
{
i = i-1;
}
else if(m_node[i][j+1].value == min-1 && m_node[i][j+1].close2==1)
{
j = j+1;
}
else if(m_node[i][j-1].value == min-1 && m_node[i][j-1].close2==1)
{
j = j-1;
}
min--;
pDC->LineTo(j * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , i * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
}
pDC->LineTo(targetX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , targetY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
}
else if(m_node[linkY][linkX].close2 == 1)//如果连接点属于end生成的
{
while(m_node[i][j].value!=0)
{
if(m_node[i+1][j].value == min-1 && m_node[i+1][j].close2==1)
{
i = i+1;
}
else if(m_node[i-1][j].value == min-1 && m_node[i-1][j].close2==1)
{
i = i-1;
}
else if(m_node[i][j+1].value == min-1 && m_node[i][j+1].close2==1)
{
j = j+1;
}
else if(m_node[i][j-1].value == min-1 && m_node[i][j-1].close2==1)
{
j = j-1;
}
min--;
pDC->LineTo(j * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , i * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
}
pDC->LineTo(targetX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , targetY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
pDC->MoveTo(linkX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , linkY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
i=linkY;
j=linkX;
if(m_node[i+1][j].close1==1)
{
min = m_node[i+1][j].value;
i = i+1;
}
else if(m_node[i-1][j].close1==1)
{
min = m_node[i-1][j].value;
i = i-1;
}
else if(m_node[i][j+1].close1==1)
{
min = m_node[i][j+1].value;
j = j+1;
}
else if(m_node[i][j-1].close1==1)
{
min = m_node[i][j-1].value;
j = j-1;
}
while(m_node[i][j].value!=0)
{
if(m_node[i+1][j].value == min-1 && m_node[i+1][j].close1==1)
{
i = i+1;
}
else if(m_node[i-1][j].value == min-1 && m_node[i-1][j].close1==1)
{
i = i-1;
}
else if(m_node[i][j+1].value == min-1 && m_node[i][j+1].close1==1)
{
j = j+1;
}
else if(m_node[i][j-1].value == min-1 && m_node[i][j-1].close1==1)
{
j = j-1;
}
min--;
pDC->LineTo(j * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , i * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
}
pDC->LineTo(startX * bmMetric2.bmWidth+bmMetric2.bmWidth/2 , startY * bmMetric2.bmHeight+bmMetric2.bmHeight/2);
}
pDC->SelectObject(p_pen);
pen.DeleteObject();
//结束描绘最短路径
}
/*
//显示搜索过的点离起点的值
CFont *poldfont,*newfont=new CFont;
TEXTMETRIC tm;
newfont->CreateFont(10,0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,"宋体");
poldfont=pDC->SelectObject(newfont);
pDC->GetTextMetrics(&tm);
for(i=0;i<Height;i++)
{
for(int j=0;j<Width;j++)
{
if(m_map[i][j]>=1)
{
char buf[4];
sprintf(buf,"%d", m_map[i][j]);
pDC->TextOut(j*bmMetric2.bmWidth+5,i*bmMetric2.bmHeight+5,buf);
}
}
}
pDC->SelectObject(poldfont);
newfont->DeleteObject();
*/
//描绘地图
for(int i = 0; i < Height; i ++)
{
for(int j = 0; j < Width; j ++)
{
if(map[i][j]==-1)
{
memDC.SelectObject(&bm1);
pDC->BitBlt(j*bmMetric1.bmHeight, i*bmMetric1.bmWidth,bmMetric1.bmWidth,bmMetric1.bmHeight,&memDC,0,0,SRCCOPY);
}
else if(map[i][j]==0)
{
memDC.SelectObject(&bm5);
pDC->BitBlt(j*bmMetric2.bmHeight, i*bmMetric2.bmWidth,bmMetric2.bmWidth,bmMetric2.bmHeight,&memDC,0,0,SRCCOPY);
}
else if(map[i][j]==-3)
{
memDC.SelectObject(&bm6);
pDC->BitBlt(j*bmMetric2.bmHeight, i*bmMetric2.bmWidth,bmMetric2.bmWidth,bmMetric2.bmHeight,&memDC,0,0,SRCCOPY);
}
}
}
memDC.SelectObject(pbm);
bm1.DeleteObject();
bm2.DeleteObject();
memDC.DeleteDC();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -