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

📄 defaultmap.cpp

📁 一个遗传算法的VC版本
💻 CPP
字号:
#include "defaultmap.h"

//////////////////////         画地图           /////////////////////////
void DefaultMap::DrawMap(HWND hwnd,HDC hdc ) 
{
	HPEN           hpen;
	hpen=CreatePen(PS_SOLID,1,RGB(0,128,128));  ///创建画笔
	SelectObject(hdc,hpen);
	for (int x = 0 ; x < DIVISIONS1 ; x++)
	for (int y = 0; y < DIVISIONS2 ; y++)
	{ Rectangle (hdc,50+ x * 70,50+ y * 70, 50+(x + 1) * 70,50+ (y + 1) * 70) ; }
	DeleteObject(hpen);                //删除画笔 
	return;
}

//////////////////////     保存删除POINT(在地图位置)到向量/////////////////////////////
void DefaultMap::SaveClickPoint()
{
	fstream outFile( "copy.text", ios_base::out );
	if ( !outFile )
	{
		cerr << "unable to open input file: "<< " -- bailing out!\n";
		return ;
	}
	if(vecpoint.size()<=0)
	{ 		return;	}
	outFile<<"#"<<"DEFAULT_MAP_NAME"<<'\n';              ///以后有用
	outFile<<"#"<<vecpoint.size()<<'\n';
	for(int n=0;n<vecpoint.size();n++)
	{	
		outFile<<vecpoint[n].x;
		outFile<<" ";	
		outFile<<vecpoint[n].y;
		outFile<<'\n';
	}		
	return;
}
void DefaultMap::DelAllPoint()
{
	if(vecpoint.size()<=0)
	{ 	return;	}
    vecpoint.clear();
	return;
}

/////////////////////           找到该点附近的点          ///////////////////////
vector<POINT> DefaultMap::FindAroundPoint(const POINT& point,int iarea)   //iarea为该点的半径
{
	POINT   temppoint;	
	vector< POINT> vecroundpoint;
	temppoint.x =point.x-51 ;// 判断点是否在有效区域
	temppoint.y =point.y-51 ;
    if(temppoint.x<0||temppoint.y<0||temppoint.x>700||temppoint.y>420)
	{  return  vecroundpoint; 	}	

	iarea=abs(iarea);
	for(int n=-iarea;n<1+iarea;n++)
	for(int m=iarea;m>-1-iarea;m--)
	{
		temppoint.x=point.x+m;
		temppoint.y=point.y+n;
		vecroundpoint.push_back( temppoint );   
		temppoint.x=point.x+n;
		temppoint.y=point.y+m;
		vecroundpoint.push_back( temppoint );  		
	}
	return vecroundpoint;
}

/////////////////       在地图上画点或将地点上以存在的点从地图上删除     //////////////////////
void DefaultMap::DrawPonit( HWND hwnd ,const POINT& point)  //在地图上画点(参数为实际位置)
{
	int cx =point.x-51 ;// 判断点是否在有效区域
	int cy =point.y-51 ;
    if(cx<0||cy<0||cx>700||cy>450)
	{   return; }	
    AddPoint( point );

	HDC  hdc;
	HPEN hpen;
	hdc=GetDC(hwnd);
	hpen=CreatePen(PS_DOT,5,RGB(255,0,0));
	SelectObject(hdc,hpen);	
	Ellipse(hdc,point.x+2,point.y+2,point.x-2,point.y-2);  ////画圆点
	DeleteObject(hpen);
	ReleaseDC (hwnd, hdc);	
	return;
}
void DefaultMap::SmearPonit( HWND hwnd ,const POINT& point)//将地图上已存在的点(参数为实际位置)删除
{                                                         //擦掉该点(重画所有的点和地图)
	HDC hdc; 
	int n=vecpoint.size();
	SubPoint(point);

	if(n==vecpoint.size())     //没找到则向量大小不变
	{ return;}
	hdc=GetDC(hwnd);
	DrawMap( hwnd, hdc );
	ReleaseDC (hwnd, hdc);
	for(int n=0;n<vecpoint.size();n++)                   ///重画所有的点和地图
	{	DrawPonit(hwnd,vecpoint[n] );	}		
	return;
}

///////////////////        添加和减少点( 在地图位置)到向量          ///////////////////////
void DefaultMap::AddPoint(const POINT& point) // 保存POINT(参数为实际位置)到向量
{	
	POINT tempoint=point;
	if(vecpoint.size()!=0 && find_if(vecpoint.begin(),vecpoint.end(),equal_point(tempoint))!=vecpoint.end())
	{ return;}                                                //保证点不重复
	vecpoint.push_back( tempoint );
	
}

void DefaultMap::SubPoint(const POINT& point)
{		
	if(vecpoint.size()<=0)
	{ 	return;	}	         
	vector< POINT> vecroundpoint;	
	vector <POINT>::iterator Iter1;	

	vecroundpoint=FindAroundPoint(point,2);         //获得该点附近的点
   if(vecroundpoint.size()==0 )
	{ return;}
	Iter1=find_first_of(vecpoint.begin(),vecpoint.end(),vecroundpoint.begin(),vecroundpoint.end(),equal_point());
	if(Iter1==vecpoint.end())
	{ return;}
	vecpoint.erase(Iter1);
}

////////////////////         获得所有已点击的点的位置    ////////////////////
vector<POINT> DefaultMap::GetAllClickPoint( ) 
{
	return vecpoint;
}
///////////////////         将地图与实际位置之间转换    ///////////////////
POINT DefaultMap::GetRealPoint(const POINT& point)    //实际位置转换为地图位置
{
	POINT temppoint;
	temppoint.x =point.x-51 ; // 判断点是否在有效区域
	temppoint.y=point.y-51 ;
    if(temppoint.x<0||temppoint.y<0||temppoint.x>700||temppoint.y>420)
	{   
		temppoint.x=-1;
		temppoint.y=-1;
		return temppoint;
	}	
	temppoint.x+=51;
	temppoint.y+=51;
	return  temppoint;
}

POINT DefaultMap::GetMapPoint(const POINT& point)    //地图位置转换为实际位置 
{
	POINT temppoint=point;
	temppoint.x-=51;
	temppoint.y-=51;
	if( temppoint.x<0 || temppoint.y<0 || 700<temppoint.x || 420<temppoint.y )
	{   
		temppoint.x=-1;
		temppoint.y=-1;
		return temppoint;
	}		
	return  temppoint;
}

⌨️ 快捷键说明

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