⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 anttspview.cpp

📁 蚁群算法解决TSP问题,界面实现,有城市,有坐标,有路线,是我自己编的
💻 CPP
字号:
// AntTSPView.cpp : implementation of the CAntTSPView class
//

#include "stdafx.h"
#include "AntTSP.h"
#include "MyTsp.h"
#include "AntTSPDoc.h"
#include "AntTSPView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int besttour[iCityCount];
extern double mindistance;//最佳路线长度
/////////////////////////////////////////////////////////////////////////////
// CAntTSPView
project AntTsp;
IMPLEMENT_DYNCREATE(CAntTSPView, CFormView)

BEGIN_MESSAGE_MAP(CAntTSPView, CFormView)
	//{{AFX_MSG_MAP(CAntTSPView)
	ON_COMMAND(ID_start, Onstart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAntTSPView construction/destruction

CAntTSPView::CAntTSPView()
	: CFormView(CAntTSPView::IDD)
{
	//{{AFX_DATA_INIT(CAntTSPView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CAntTSPView::~CAntTSPView()
{
}

void CAntTSPView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAntTSPView)
	DDX_Control(pDX, ID_t, m_t);
	DDX_Control(pDX, IDC_S, m_s);
	//}}AFX_DATA_MAP
}

BOOL CAntTSPView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CAntTSPView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CAntTSPView diagnostics

#ifdef _DEBUG
void CAntTSPView::AssertValid() const
{
	CFormView::AssertValid();
}

void CAntTSPView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CAntTSPDoc* CAntTSPView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAntTSPDoc)));
	return (CAntTSPDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAntTSPView message handlers

void CAntTSPView::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
	CFont nFont;
	nFont.CreateFont(25,0,0,0,1,0,0,0,0,0,0,0,0,"楷体_GB2312");
	m_s.SetFont(&nFont,true);
	m_s.SetWindowPos(NULL,200,30,700,80,1);	
	int i;
	    POINT temp;
    	CString strNumber;
	    CBrush myrush,*oldrush;
	    myrush.CreateSolidBrush(RGB(248,55,18));
	    if(besttour[0]==0&&besttour[1]==0)
		{	oldrush=pDC->SelectObject(&myrush);
	      	for(i=0; i<iCityCount; i++)
			{
		    	temp.x = 10*AntTsp.cc[i].x+200;
		    	temp.y = 7*AntTsp.cc[i].y+80;
		    	pDC->SetPixel(temp,RGB(0,0,255));
		    	pDC->Ellipse(temp.x-5,temp.y-5,temp.x+5,temp.y+5);
			    strNumber.Format("%d",AntTsp.cc[i].num);
			    pDC->TextOut(temp.x-15,temp.y-24,strNumber);
			}
	    	pDC->SelectObject(oldrush);
		}
    	else
		{
	    	
	    	CPen npen,*oldpen;
		    npen.CreatePen(PS_DASH,2,RGB(0,0,255));
		    for(i=0; i<iCityCount-1; i++)
			{
			 temp.x = 10*AntTsp.cc[besttour[i]].x+200;
			 temp.y = 7*AntTsp.cc[besttour[i]].y+80;
			 oldrush=pDC->SelectObject(&myrush);
			 pDC->Ellipse(temp.x-5,temp.y-5,temp.x+5,temp.y+5);
		   	 pDC->SelectObject(oldrush);
			 oldpen=pDC->SelectObject(&npen);
			 pDC->MoveTo(temp);
			 temp.x = 10*AntTsp.cc[besttour[i+1]].x+200;
			 temp.y = 7*AntTsp.cc[besttour[i+1]].y+80;
			 pDC->LineTo(temp);
			 strNumber.Format("%d",AntTsp.cc[besttour[i]].num);
			 pDC->TextOut(temp.x,temp.y,strNumber);
			 pDC->SelectObject(oldpen);
			}
		    strNumber.Format("%d",AntTsp.cc[besttour[iCityCount-1]].num);
		    pDC->TextOut(temp.x,temp.y,strNumber);

		    strNumber.Format("E");
		    pDC->TextOut(10*AntTsp.cc[besttour[iCityCount-1]].x+200-5,7*AntTsp.cc[besttour[iCityCount-1]].y+80+5,strNumber);
		    strNumber.Format("S");
		    pDC->TextOut(10*AntTsp.cc[besttour[0]].x+200-5,7*AntTsp.cc[besttour[0]].y+80+5,strNumber);
		    	CString out="最优路径:",s;
				s.Format("%d",AntTsp.cc[besttour[0]].num);
			    out+=s;
            	for (i=1;i<iCityCount;i++)
				{	
					s.Format("-%d",AntTsp.cc[besttour[i]].num);
			        out+=s;
				}
			m_t.SetWindowText(out);	
			out.Format("最终得出的最短路径,本次求出的最优距离:%f",mindistance);
			m_s.SetWindowText(out);
		}			
}

void CAntTSPView::Onstart() 
{
	// TODO: Add your command handler code here
    m_s.SetWindowText("程序正在加载相关数据,请稍候-------");
	AntTsp.GetAnt();
	m_s.SetWindowText("正在搜索最短路径,可能需要几分钟时间,请稍候-------");
    AntTsp.StartSearch();
	Invalidate();	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -