📄 pathview.cpp
字号:
if(pDoc->m_cBoard[i+jj][j])
jj=5;
if(pDoc->m_cBoard[i-jj][j])
jj=5;
}
if(jj==4)
{
pt1 = CPoint(px,py);
//pt1 = CPoint(px+6,k2*(px+6)+b2);
ii = 40;
}
}
for(ii=1;ii<40;ii++)
{
float px = middle.x - 6*ii;
float py = middle.y;
int i=px/MAP_GRIDSIZE;
int j=py/MAP_GRIDSIZE;
for(int jj=1;jj<4;jj++)
{
if(pDoc->m_cBoard[i][j+jj])
jj=5;
if(pDoc->m_cBoard[i][j-jj])
jj=5;
if(pDoc->m_cBoard[i+jj][j])
jj=5;
if(pDoc->m_cBoard[i-jj][j])
jj=5;
}
if(jj==4)
{
pt2 = CPoint(px,py);
//pt2 = CPoint(px-6,k2*(px+6)+b2);
ii = 40;
}
}
//计算两个点与中间点的距离,取小的
CheckRightLeft(now);
float ds1,ds2;
if(flagRight && !flagLeft)
{
ds1=pDoc->ComDistance(pt1,middle);
pDoc->m_cEndArray.Add(pt1);
pDoc->SetNewEndPoint(pt1);
SetTimer(1,150,NULL);
return;
}
if(flagLeft && !flagRight)
{
ds2=pDoc->ComDistance(pt2,middle);
pDoc->m_cEndArray.Add(pt2);
pDoc->SetNewEndPoint(pt2);
SetTimer(1,150,NULL);
return;
}
ds1=pDoc->ComDistance(pt1,middle);
ds2=pDoc->ComDistance(pt2,middle);
if(ds1<=ds2)
{
newend=pt1;
}
else
{
newend=pt2;
}
//***********
pDoc->m_cEndArray.Add(newend);
pDoc->SetNewEndPoint(newend);
SetTimer(1,150,NULL);
return;
/*pDoc->k2 = 0;
pDoc->b2 = now.y;
if(now.y > end.y)
{
pDoc->m_cMiddle.x = now.x ;
pDoc->m_cMiddle.y = now.y - 16 ;
}
else
{
pDoc->m_cMiddle.x = now.x ;
pDoc->m_cMiddle.y = now.y + 16 ;
}
pDoc->CheckAlongLine2(pDoc->k2,pDoc->b2,pDoc->m_cMiddle,end,newend);
pDoc->m_cEndArray.Add(newend);
pDoc->SetNewEndPoint(newend);
SetTimer(1,150,NULL);
return;
}
if(absDY<3 && absDX>10)
{
//**********
bool flag;
if(end.x > now.x)
flag=true;
else
flag=false;
CPoint middle;
for(int ii=1;ii<20;ii++)
{
float px;
if(flag)
px = now.x + 6*ii;
else
px = now.x - 6*ii;
//float px = now.x + 6*ii;
float py = now.y;
int i=px/MAP_GRIDSIZE;
int j=py/MAP_GRIDSIZE;
if(pDoc->m_cBoard[i][j]==1)
{
if(flag)
middle.x = px + 4;
else
middle.x = px - 4;
middle.y = py;
ii=22;
}
}
//CheckUpDown();
CPoint pt1,pt2;
for(ii=1;ii<40;ii++)
{
float py = middle.y + 6*ii;
float px = middle.x;
int i=px/MAP_GRIDSIZE;
int j=py/MAP_GRIDSIZE;
for(int jj=1;jj<4;jj++)
{
if(pDoc->m_cBoard[i][j+jj])
jj=5;
if(pDoc->m_cBoard[i][j-jj])
jj=5;
if(pDoc->m_cBoard[i+jj][j])
jj=5;
if(pDoc->m_cBoard[i-jj][j])
jj=5;
}
if(jj==4)
{
pt1 = CPoint(px,py);
//pt1 = CPoint(px+6,k2*(px+6)+b2);
ii = 40;
}
}
for(ii=1;ii<40;ii++)
{
float py = middle.y - 6*ii;
float px = middle.x;
int i=px/MAP_GRIDSIZE;
int j=py/MAP_GRIDSIZE;
for(int jj=1;jj<4;jj++)
{
if(pDoc->m_cBoard[i][j+jj])
jj=5;
if(pDoc->m_cBoard[i][j-jj])
jj=5;
if(pDoc->m_cBoard[i+jj][j])
jj=5;
if(pDoc->m_cBoard[i-jj][j])
jj=5;
}
if(jj==4)
{
pt2 = CPoint(px,py);
//pt2 = CPoint(px-6,k2*(px+6)+b2);
ii = 40;
}
}
//计算两个点与中间点的距离,取小的
CheckUpDown(now);
float ds1,ds2;
if(flagUp && !flagDown)
{
ds1=pDoc->ComDistance(pt1,middle);
pDoc->m_cEndArray.Add(pt1);
pDoc->SetNewEndPoint(pt1);
SetTimer(1,150,NULL);
return;
}
if(flagDown && !flagUp)
{
ds2=pDoc->ComDistance(pt2,middle);
pDoc->m_cEndArray.Add(pt2);
pDoc->SetNewEndPoint(pt2);
SetTimer(1,150,NULL);
return;
}
ds1=pDoc->ComDistance(pt1,middle);
ds2=pDoc->ComDistance(pt2,middle);
if(ds1<=ds2)
{
newend=pt1;
}
else
{
newend=pt2;
}
//***********
pDoc->m_cEndArray.Add(newend);
pDoc->SetNewEndPoint(newend);
SetTimer(1,150,NULL);
return;
}*/
/*
pDoc->CalculateLine1(now,end);
pDoc->CheckAlongLine1(pDoc->k1,pDoc->b1,now,end,pDoc->m_cMiddle);
pDoc->CalculateLine2(pDoc->m_cMiddle,pDoc->k1);
pDoc->CheckAlongLine2(pDoc->k2,pDoc->b2,pDoc->m_cMiddle,end,newend);
pDoc->m_cEndArray.Add(newend);
pDoc->SetNewEndPoint(newend);
SetTimer(1,150,NULL);
return;
*/
//GoAlongWall(m_cNow);
//////pDoc->ResetArray();
//////m_cMiddle.x = m_cNow.x / MAP_GRIDSIZE;
//////m_cMiddle.y = m_cNow.y / MAP_GRIDSIZE;
//////SetTimer(2,250,NULL);
//GetDirection(m_cNow,end);
//CPoint temp=m_cNow;
//while(!CheckPointStyle(temp));
//newend=CPoint(100,100);
//pDoc->m_cEndArray.Add(newend);
//pDoc->SetNewEndPoint(newend);
//SetTimer(1,150,NULL);
}
}
}
else
{
KillTimer(1);
//pDoc->MessageBox("可以生成避碰路径!","APF",MB_ICONINFORMATION);
}
CView::OnTimer(nIDEvent);
}
void CPathView::GoAlongWall(CPoint now)
{
CPathDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->CheckCurrentPoint(now);//设置相应的标志
CPoint temp;
temp = pDoc->GetNextPoint(now);//计算下一个要移动到的节点
m_cMiddle = temp;
temp.x = temp.x * MAP_GRIDSIZE;
temp.y = temp.y * MAP_GRIDSIZE;
m_cNow = temp;
pDoc->m_cPathArray.Add(m_cNow);
}
//***** 绘制作用区间 *****
void CPathView::DrawCircle(int x, int y, CDC *pDC, COLORREF cr,int radius)
{
CBrush br(cr), *oldbrush;
CPen pen(PS_SOLID, 0, RGB(0,0,0)), *oldpen;
CRect rect(x-radius, y-radius,
x+radius,
y+radius);
oldbrush = pDC->SelectObject(&br);
pDC->SetROP2(R2_NOT);
pDC->SetMapMode(TRANSPARENT);
pDC->SelectStockObject(NULL_BRUSH);
oldpen = pDC->SelectObject(&pen);
pDC->Ellipse(rect);
pDC->SelectObject(oldbrush);
pDC->SelectObject(oldpen);
}
//***** 绘制路径 *****
void CPathView::DrawRoute(CDC *pDC)
{
CPathDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPen pen(PS_SOLID,1,RGB(0,0,0)),*oldpen;
oldpen = pDC->SelectObject(&pen);
int x,y;
CPoint pt;
for(int i=0;i<pDoc->m_cPathArray.GetSize ()-1 ;i++)
{
if(i==0)
{
pt.x=pDoc->m_cPathArray[i].x;
pt.y=pDoc->m_cPathArray[i].y;
DrawCircle(pt.x,pt.y,pDC,RGB(0,0,0),K_DISTANCE);
}
else
{
DrawCircle(pt.x,pt.y,pDC,RGB(0,0,0),K_DISTANCE);
pDC->MoveTo(pt);
x = pDoc->m_cPathArray[i].x;
y = pDoc->m_cPathArray[i].y;
DrawCircle(x,y,pDC,RGB(0,0,0),K_DISTANCE);
pt.x=x;
pt.y=y;
pDC->LineTo(pt);
}
}
pDC->SelectObject(oldpen);
}
//***** 操作鼠标 *****
void CPathView::MouseToPoint(CPoint point, UINT brush)
{
CClientDC dc(this);
int px = point.x/8, py = point.y/8;//取整
CPoint round(px*MAP_GRIDSIZE, py*MAP_GRIDSIZE),temp;
GetDocument()->GetStartEnd(m_cStart, m_cEnd);
// If start or end
if (brush == 2) {
//开始节点
temp.x = m_cStart.x * MAP_GRIDSIZE;
temp.y = m_cStart.y * MAP_GRIDSIZE;
m_cStart.x = px;
m_cStart.y = py;
InvalidateRect(CRect(round,CSize(MAP_GRIDSIZE+1,MAP_GRIDSIZE+1)), false);
InvalidateRect(CRect(temp,CSize(MAP_GRIDSIZE+1,MAP_GRIDSIZE+1)), false);
} else if (brush == 3) {
//目标节点
temp.x = m_cEnd.x * MAP_GRIDSIZE;
temp.y = m_cEnd.y * MAP_GRIDSIZE;
m_cEnd.x = px;
m_cEnd.y = py;
InvalidateRect(CRect(round,CSize(MAP_GRIDSIZE+1,MAP_GRIDSIZE+1)), false);
InvalidateRect(CRect(temp,CSize(MAP_GRIDSIZE+1,MAP_GRIDSIZE+1)), false);
} else {
char *board = GetDocument()->GetBoard();
board[ci(px,py)] = brush;// 0 或 1
InvalidateRect(CRect(round,CSize(MAP_GRIDSIZE+1,MAP_GRIDSIZE+1)), false);
}
GetDocument()->SetStartEnd(m_cStart, m_cEnd);
}
void CPathView::OnLButtonDown(UINT nFlags, CPoint point)
//根据所选画刷颜色设置界面矩形块
{
// TODO: Add your message handler code here and/or call default
MouseToPoint(point, m_uBrushType);
if (m_uBrushType < 2) m_bDragging = true;//设置或清除障碍,允许拖动
CView::OnLButtonDown(nFlags, point);
}
void CPathView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bDragging) {
MouseToPoint(point, m_uBrushType);
}
CView::OnMouseMove(nFlags, point);
}
void CPathView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bDragging = false;
CView::OnLButtonUp(nFlags, point);
}
BOOL CPathView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return false;
return CView::OnEraseBkgnd(pDC);
}
void CPathView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
GetDocument()->GetStartEnd(m_cStart, m_cEnd);
m_cNow=m_cStart;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -