📄 航路规划view.cpp
字号:
// 航路规划View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "航路规划.h"
#include "航路规划Doc.h"
#include "航路规划View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "inputdialog.h"
#include "math.h"
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_COMMAND(input, Oninput)
ON_COMMAND(start, Onstart)
ON_EN_MAXTEXT(IDC_ED, OnMaxtextEd)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
struct obstaclenode * obp;
struct node * closepointer,*cp1,*cp2,
*openhead,*op1,*op2,
*expandinghead,*expandingpointer,*ep1,*ep2,
*routepointer;
int n=0;
int a,b;
if(pDoc->startsearch==1)
{///////////////////////////////////////////////////////////////////////////
//close,open,expanding链表初始化
struct node node[7];
for(int i=0;i<7;i++)
{
node[i].x=0;
node[i].y=0;
node[i].docost=0;
node[i].undocost=0;
node[i].fathernodepointer=NULL;
}
cp1=&node[0];
cp2=closepointer=&node[1];
cp1->next=cp2;
cp2->next=NULL;
ep1=&node[2];
ep2=&node[3];
ep1->next=ep2;
ep2->next=NULL;
op1=&node[4];
op2=&node[5];
openhead=&node[6];
op1->next=op2;
op2->next=openhead;
node[6].x=pDoc->startx;
node[6].y=pDoc->starty;
node[6].undocost=sqrt(500*500+900*900);
node[6].fathernodepointer=NULL;
node[6].next=NULL;
//循环取open首点进行扩展
while(openhead!=NULL)
{
/////////////////////////////////////////////////////////
// open首点移入close链表
closepointer->next=openhead;
closepointer=openhead;
openhead=openhead->next;
node[5].next=openhead;
closepointer->next=NULL;
///////////////////////////////////////////////////////////
//建立expanding链表(第一次合法性检查)
expandingpointer=&node[3];
for (int j=0; j<3; j++)
{
switch(j)
{
case(0):a=closepointer->x+1;b=closepointer->y-1;
break;
case(1):a=closepointer->x+1;b=closepointer->y;
break;
case(2):a=closepointer->x+1;b=closepointer->y+1;
break;
}
n=0;
obp=(pDoc->obstaclepreviousnode).next; /*传入obstacle链表头指针*/
while(obp!=NULL)
{
if((obp->x-a)*(obp->x-a)+(obp->y-b)*(obp->y-b)<=obp->r*obp->r)
{
n=1;
break;
}
obp=obp->next;
}
if(n==1)
continue;
if(a>=pDoc->endx+1)
goto display1; /*超界检查*/
expandingpointer->next=(struct node*)malloc(sizeof(struct node)); /*分配内存空间*/
expandingpointer=expandingpointer->next;/*链入expanding表*/
expandingpointer->x=a;/*节点赋值*/
expandingpointer->y=b;
switch(j)
{
case(0):expandingpointer->docost=closepointer->docost+1.4f;
break;
case(1):expandingpointer->docost=closepointer->docost+1; /*可取二维五叉树,三叉树或三维搜索*/
break;
case(2):expandingpointer->docost=closepointer->docost+1.4f;
break;
}
expandingpointer->undocost=fabs(5*a+9*b-5900)/sqrt(106)+
sqrt((pDoc->endx-a)*(pDoc->endx-a)+(pDoc->endy-b)*(pDoc->endy-b));
expandingpointer->fathernodepointer=closepointer;
expandingpointer->next=NULL;
if(a==pDoc->endx && b==pDoc->endy)
{
pDoc->startsearch=0;
goto display2;/*搜索结束跳出并显示*/
}
}
/////////////////////////////////////////////////////////
//expanding节点第二次检查
ep1=&node[2];
ep2=&node[3];
while(ep2->next!=NULL)
{
ep1=ep1->next;
loop: ep2=ep1->next;
//////////////////////
//比较close链表
cp1=&node[0];
cp2=&node[1];
n=0;
while(cp2->next!=NULL)
{
cp1=cp1->next;
cp2=cp1->next;
if(cp2->x==ep2->x && cp2->y==ep2->y)
{
n=1;
break;
}
}
if(n==1)
{
if(cp2->docost<=ep2->docost)
{
ep1->next=ep2->next;
free(ep2); /*expanding节点删除*/
if(ep1->next!=NULL) /*校正expanding指针*/
goto loop;
else break;
}
else
{
cp1->next=cp2->next;
free(cp2); /*close节点删除*/
continue;
}
}
/////////////////////
//比较open链表
op1=&node[4];
op2=&node[5];
n=0;
while(op2->next!=NULL)
{
op1=op1->next;
op2=op1->next;
if(op2->x==ep2->x && op2->y==ep2->y)
{
n=1;
break;
}
}
if(n==1)
{
if(op2->docost<=ep2->docost)
{
ep1->next=ep2->next;
free(ep2); /*expanding节点删除*/
if(ep1->next!=NULL) /*校正expanding指针*/
goto loop;
else break;
}
else
{
op1->next=op2->next;
free(op2); /*open节点删除*/
}
}
}
//////////////////////////////////////////////////////
//把expanding节点从小到大插入已有open链表中
while( node[3].next != NULL )
{
expandinghead=node[3].next;
node[3].next=expandinghead->next;
op1=&node[4];
op2=&node[5];
n=0;
while(op2->next!=NULL)
{
op1=op1->next;
op2=op1->next;
if(0.4*expandinghead->docost+0.6*expandinghead->undocost<=0.4*op2->docost+0.6*op2->undocost) /*g(n),h(n)选择设置*/
{
n=1;
break;
}
}
if(n==1)
{
op1->next=expandinghead;
expandinghead->next=op2;
}
else
{
op2->next=expandinghead;
expandinghead->next=NULL;
}
}
openhead=node[5].next;
}
///////////////////////////////////////////////////////////////////
//总循环跳出
display1: pDC->TextOut(400,300,"规划失败");
display2: routepointer=expandingpointer;
CPen tPen;
tPen.CreatePen(PS_SOLID, 1, RGB(0,255,0));
CPen * tOldPen;
tOldPen = pDC->SelectObject(&tPen);
pDC->MoveTo(routepointer->x,routepointer->y);
while(routepointer->fathernodepointer!=NULL)
{
routepointer=routepointer->fathernodepointer;
pDC->LineTo(routepointer->x,routepointer->y);
}
pDC->SelectObject(tOldPen);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//if选择绘路径结束,开始画障碍圆
pDC->SelectStockObject(BLACK_BRUSH);
obp=(pDoc->obstaclepreviousnode).next; /*传入obstacle链表头指针*/
while(obp!=NULL)
{
pDC->Ellipse(obp->x-obp->r,obp->y-obp->r,obp->x+obp->r,obp->y+obp->r);
obp=obp->next;
}
TRACE ("规划失败"规划失败"");
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
////////////////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::Oninput()
{ CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: Add your command handler code here
inputdialog dlg;
if(dlg.DoModal()==IDOK)
{
(pDoc->obstaclepointer)->next=(struct obstaclenode*)malloc(sizeof(struct obstaclenode));
(pDoc->obstaclepointer)=(pDoc->obstaclepointer)->next;
(pDoc->obstaclepointer)->x=dlg.x;
(pDoc->obstaclepointer)->y=dlg.y;
(pDoc->obstaclepointer)->r=dlg.r;
(pDoc->obstaclepointer)->next=NULL;
}
Invalidate(FALSE);
}
void CMyView::Onstart()
{ CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: Add your command handler code here
pDoc->startsearch=1;
Invalidate(FALSE);
}
void CMyView::OnMaxtextEd()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -