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

📄 setmap.cpp

📁 寻找最短路系统vc实现
💻 CPP
字号:
// SetMap.cpp : implementation file
//

#include "stdafx.h"
#include "找最短路系统.h"
#include "SetMap.h"
#include "Distance.h"
#include "SaveMap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define maxdist 1000000
/////////////////////////////////////////////////////////////////////////////
// CSetMap dialog


CSetMap::CSetMap(CWnd* pParent /*=NULL*/)
	: CDialog(CSetMap::IDD, pParent)
{
	bSetPoint=false;
	bSetDistance=false;
	curColor=RGB(255,0,0);
	mapRect=CRect(0,0,0,0);
	line_start=false;
	bmodify_distance=false;
	bclear_point=bclear_line=false;
	//{{AFX_DATA_INIT(CSetMap)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CSetMap::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetMap)
	DDX_Control(pDX, IDC_CLEAR_LINE, m_clear_line);
	DDX_Control(pDX, IDC_CLEAR_POINT, m_clear_point);
	DDX_Control(pDX, IDC_MODIFY_DISTANCE, m_modify_distance);
	DDX_Control(pDX, IDC_INPUT_POINT, m_input_point);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetMap, CDialog)
	//{{AFX_MSG_MAP(CSetMap)
	ON_BN_CLICKED(IDC_OKK, OnOkk)
	ON_BN_CLICKED(IDC_CANCELL, OnCancell)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_BN_CLICKED(IDC_INPUT_POINT, OnInputPoint)
	ON_BN_CLICKED(IDC_INPUT_DISTANCE, OnInputDistance)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_MODIFY_DISTANCE, OnModifyDistance)

	ON_BN_CLICKED(IDC_CLEAR_POINT, OnClearPoint)
	ON_BN_CLICKED(IDC_CLEAR_LINE, OnClearLine)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetMap message handlers

void CSetMap::OnOkk() 
{
	// TODO: Add your control notification handler code here
	CSaveMap saveDlg;
	if(saveDlg.DoModal()==IDOK){
			CFileDialog mdlg(1,"*.txt");
			mdlg.DoModal();
			int i,len=mdlg.GetPathName().GetLength();

			CString tempstr;
			char *filename;
			filename=new char[len+1];
			for(i=0;i<len;i++){
				filename[i]=mdlg.GetPathName().GetAt(i);
			}
			filename[len]=0;

			CFile infile;
			infile.Open(filename,1);
			if(!infile)	MessageBox("open failed");
			infile.Write(&point_nums,sizeof(int));
			infile.Write(&line_nums,sizeof(int));
			for(i=0;i<point_nums;i++){
				infile.Write(&points[i].x,sizeof(int));
				infile.Write(&points[i].y,sizeof(int));
			}
			for(i=0;i<point_nums;i++){
				for(int j=0;j<point_nums;j++){
					infile.Write(&distances[i][j],sizeof(double));
				}
			}
	}
	OnOK();
}

void CSetMap::OnCancell() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CSetMap::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnMouseMove(nFlags, point);
}

void CSetMap::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(bSetDistance){	
		line_start=true;
		startp=GetPoint(point);
	
	}else if(bmodify_distance){
		double lmaxdis,tempdis;
		int u,v;
		u=v=-1;
		lmaxdis=maxdist;
		for(int  i=0;i<100;i++){
			for(int j=i+1;j<100;j++){
				if(distances[i][j]!=maxdist){
					int midx=(points[i].x-points[j].x)/2+points[j].x;
					int midy=(points[i].y-points[j].y)/2+points[j].y;
					tempdis=(point.x-midx)*(point.x-midx)+
						(point.y-midy)*(point.y-midy);
					if(tempdis<lmaxdis){
						lmaxdis=tempdis;
						u=i;v=j;
					}
				}
			}
		}
		if(u!=-1){
			CDistance distanceDlg;
			distanceDlg.DoModal();
			if(distanceDlg.m_distance){
				distances[u][v]=
				distances[v][u]=
				    distanceDlg.m_distance;
			}
			OnPaint();
		}
	}
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CSetMap::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	CBrush *mBrush,*oldBrush;
	if(bSetPoint){
		if(point.x>map.right-map.left||point.y>map.bottom-map.top||
			point.x<10||point.y<10)return ;
		points[point_nums++]=point;
		mBrush=new CBrush(curColor);
		oldBrush=dc.SelectObject(mBrush);
		dc.Ellipse(point.x-3,point.y-3,point.x+3,point.y+3);
		dc.SelectObject(oldBrush);
		delete mBrush;		
	}else if(bSetDistance){
		if(line_start){
			endp=GetPoint(point);		
			if((points[startp].x)!=(points[endp].x)||(points[startp].y)!=(points[endp].y)){
				CPen *mpen,*oldpen;
				mpen=new CPen(PS_SOLID,1,RGB(0,0,255));
				CClientDC dc(this);
				oldpen=dc.SelectObject(mpen);
				dc.MoveTo(points[startp].x,points[startp].y);
				dc.LineTo(points[endp].x,points[endp].y);
				dc.SelectObject(oldpen);
				delete mpen;
				CDistance distanceDlg;
				distanceDlg.DoModal();
				if(distanceDlg.m_distance){
					distances[endp][startp]=
						distances[startp][endp]=
						    distanceDlg.m_distance;
					line_nums++;
				}
				this->UpdateData(false);
			
			}
			line_start=false;
			endp=startp=0;
			UpdateData(false);
			
		}
	}
	CDialog::OnLButtonUp(nFlags, point);
	OnPaint();
}

void CSetMap::OnInputPoint() 
{
	// TODO: Add your control notification handler code here
	if(bSetPoint){
        bSetPoint=false;
		bSetDistance=true;
		m_input_point.SetCheck(0);
	}else{
		bSetPoint=true;
		bSetDistance=false;
		m_input_point.SetCheck(1);
	}
}

void CSetMap::OnInputDistance() 
{
	// TODO: Add your control notification handler code here
	if(bSetDistance){        
		bSetPoint=true;
		bSetDistance=false;
		m_input_point.SetCheck(1);
	}else{
		bSetPoint=false;
		bSetDistance=true;
		m_input_point.SetCheck(0);
	}
}

int CSetMap::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	return 0;
}

BOOL CSetMap::OnInitDialog() 
{
	CDialog::OnInitDialog();	
	// TODO: Add extra initialization here

	m_input_point.SetCheck(1);
	bSetPoint=true;
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int CSetMap::GetPoint(POINT p)
{
   int i,index=0;
   double d=0,min=10000000;
   for(i=0;i<point_nums;i++){
		d=(p.x-points[i].x)*(p.x-points[i].x)+
				(p.y-points[i].y)*(p.y-points[i].y);
		if(d<min){
			index=i;
			min=d;
		}
   }
   return index;
}
void CSetMap::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	CString strdis;

	// TODO: Add your message handler code here
//	CClientDC dc(this);				
		CBrush *mbrush=new CBrush(RGB(0,128,0)),*oldbrush;
		oldbrush=dc.SelectObject(mbrush);
		dc.Rectangle(10,10,map.right-map.left,map.bottom-map.top);		
		dc.SelectObject(oldbrush);
		delete mbrush;

		mbrush=new CBrush(RGB(255,0,0));
		oldbrush=dc.SelectObject(mbrush);
		for(int i=0;i<point_nums;i++){
			dc.Ellipse(points[i].x-3,points[i].y-3,points[i].x+3,points[i].y+3);
		}
		dc.SelectObject(oldbrush);
		delete mbrush;
		CPen *mpen=new CPen(PS_SOLID,1,RGB(0,0,255)),*oldpen;
		oldpen=dc.SelectObject(mpen);
		for(i=0;i<100;i++){
			for(int j=i+1;j<100;j++){
				if(distances[i][j]!=maxdist){
					dc.MoveTo(points[i].x,points[i].y);
					dc.LineTo(points[j].x,points[j].y);
					int midx=(points[i].x-points[j].x)/2+points[j].x;
					int midy=(points[i].y-points[j].y)/2+points[j].y;
					strdis.Format("%.2f",distances[i][j]);
					dc.SetBkColor(RGB(0,128,0));
					dc.SetTextColor(RGB(255,128,0));
					dc.TextOut(midx-2,midy-2,strdis);
				}
			}
		}
		dc.SelectObject(oldpen);
		delete mpen;
		UpdateData(true);
	// Do not call CDialog::OnPaint() for painting messages
}

void CSetMap::OnModifyDistance() 
{
	// TODO: Add your control notification handler code here
	if(!bmodify_distance){
		ResetAll();
		bmodify_distance=true;
		m_modify_distance.SetWindowText("结束修改");
	}else{
	   ResetAll();
	}
	UpdateData(false);

}

void CSetMap::OnClearPoint() 
{
	// TODO: Add your control notification handler code here
	if(!bclear_point){
		ResetAll();
		bclear_point=true;
		m_clear_point.SetWindowText("结束清除");
	}else{
	   ResetAll();
	}
	UpdateData(false);
}

void CSetMap::OnClearLine() 
{
	// TODO: Add your control notification handler code here
	if(!bclear_line){
		ResetAll();
		bclear_line=true;
		m_clear_line.SetWindowText("结束清除");
	}else{
	   ResetAll();
	}
	UpdateData(false);
}

void CSetMap::ResetAll()
{
	bSetPoint=false;
	bSetDistance=false;	
	line_start=false;
	bmodify_distance=false;
	bclear_point=false;
	bclear_line=false;
	m_modify_distance.SetWindowText("修改距离");

	m_clear_point.SetWindowText("清除点");
	m_clear_line.SetWindowText("清除边");
   
}

⌨️ 快捷键说明

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