?? antcolonyview.cpp
字號:
// AntColonyView.cpp : implementation of the CAntColonyView class
//
#include "stdafx.h"
#include "AntColony.h"
#include "operation.h"
#include "AntColonyDoc.h"
#include "AntColonyView.h"
#include "MainFrm.h"
#include "SETOBSDIA.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAntColonyView
SETOBSDIA asetobsdia;
unsigned int MapTemp[mapH][mapV];//臨時地圖數據
int ObsState,BottonDownFlag=0;;
void MapOut(void)
{ FILE *fp;
int i,j;
fp=fopen("map.dat", "w");
for(i=0;i<mapH;i++)
{for(j=0;j<mapV;j++)
{
fprintf(fp, "%5d",MapTemp[i][j]);
fprintf(fp, " ");
}
fprintf(fp,"\n");
}
fclose(fp) ;
}
void MapIn2(void)
{ FILE *fp1;
int i,j;
fp1=fopen("map.dat", "r");
for(i=0;i<mapH;i++)
for(j=0;j<mapV;j++)
fscanf(fp1,"%5d",&MapTemp[i][j]);
fclose(fp1) ;
}
/////////////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNCREATE(CAntColonyView, CView)
BEGIN_MESSAGE_MAP(CAntColonyView, CView)
//{{AFX_MSG_MAP(CAntColonyView)
ON_COMMAND(ID_SETOBS, OnSetobs)
ON_COMMAND(ID_MOVEOBS, OnMoveobs)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_CMAP, OnCmap)
ON_COMMAND(ID_AUTOOBSSET, OnAutoobsset)
ON_COMMAND(ID_SHOWLASTMAP, OnShowlastmap)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAntColonyView construction/destruction
CAntColonyView::CAntColonyView()
{
// TODO: add construction code here
}
CAntColonyView::~CAntColonyView()
{
}
BOOL CAntColonyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CAntColonyView drawing
void CAntColonyView::OnDraw(CDC* pDC)
{
CAntColonyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CAntColonyView diagnostics
#ifdef _DEBUG
void CAntColonyView::AssertValid() const
{
CView::AssertValid();
}
void CAntColonyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CAntColonyDoc* CAntColonyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAntColonyDoc)));
return (CAntColonyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAntColonyView message handlers
void CAntColonyView::test()
{
MapTemp[0][1]=3;
}
void CAntColonyView::OnSetobs()
{
// TODO: Add your command handler code here
ObsState=1; //允許設置障礙
}
void CAntColonyView::OnMoveobs()
{
// TODO: Add your command handler code here
ObsState=2;//允許擦除障礙
}
void CAntColonyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// StartPt.x=point.x;
// StartPt.y=point.y;
BottonDownFlag=1;
CView::OnLButtonDown(nFlags, point);
}
void CAntColonyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// StartPt.x=-1;
BottonDownFlag=0;
CView::OnLButtonUp(nFlags, point);
}
void CAntColonyView::OnMouseMove(UINT nFlags, CPoint point)
{
unsigned int x0,y0,x1,y1,gys;
gys=400/mapH;
x0=gys*(point.x/gys);
y0=gys*(point.y/gys);
x1=gys*(point.x/gys)+gys;
y1=gys*(point.y/gys)+gys;
CClientDC dc(this);
CBrush newbrush;
CBrush *oldbrush;
newbrush.CreateSolidBrush(RGB(0,0,250));
// newbrush.CreateHatchBrush(HS_CROSS,RGB(0,0,255));
// newbrush.CreateHatchBrush(HS_BDIAGONAL,RGB(0,0,255));
oldbrush=dc.SelectObject(&newbrush);
if(BottonDownFlag==1&&point.x<400&&point.y<400)//鼠標點在地圖范圍之內
{
dc.Rectangle(x0,y0,x1,y1);
MapTemp[point.y/gys][point.x/gys]=1;
}
dc.SelectObject(oldbrush);
newbrush.DeleteObject();
CView::OnMouseMove(nFlags, point);
}
void CAntColonyView::OnCmap()
{
// TODO: Add your command handler code here
MapOut();
}
void CAntColonyView::OnAutoobsset()
{
// TODO: Add your command handler code here
//取得障礙率,生成地圖,輸出地圖,畫出地圖
static int flag=0;
double prob1,prob2;
int i,j;
unsigned int x0,y0,x1,y1,gys;
if(!flag)
{
asetobsdia.m_AUTOSETOBS=10;
}
flag++;
asetobsdia.DoModal();
prob1=asetobsdia.m_AUTOSETOBS/100;
for(i=0;i<mapH;i++)
{
srand( (unsigned)time( NULL )*(i+1+rand()));
for(j=0;j<mapV;j++)
{
prob2=rand()/(double)RAND_MAX;
if(prob2<=prob1)
MapTemp[i][j]=1;
}
}
////////////////////////////////////////TT SS 不能是障礙
MapTemp[TT/mapH][TT%mapH]=0;
MapTemp[SS/mapH][SS%mapH]=0;
MapTemp[0][0]=1;
////////////////////////////////////////
CClientDC dc(this);
CBrush newbrush;
CBrush *oldbrush;
newbrush.CreateSolidBrush(RGB(0,0,255));
// newbrush.CreateHatchBrush(HS_CROSS,RGB(0,0,255));
// newbrush.CreateHatchBrush(HS_BDIAGONAL,RGB(0,0,255));
for(i=0;i<mapH;i++)
for(j=0;j<mapV;j++)
{
if(MapTemp[j][i]==1)
{
gys=400/mapH;
x0=gys*i;
y0=gys*j;
x1=gys*i+gys;
y1=gys*j+gys;
oldbrush=dc.SelectObject(&newbrush);
dc.Rectangle(x0,y0,x1,y1);
}
}
dc.SelectObject(oldbrush);
newbrush.DeleteObject();
MapOut();
}
void CAntColonyView::OnShowlastmap()
{
// TODO: Add your command handler code here
unsigned int x0,y0,x1,y1,gys;
int i,j;
CClientDC dc(this);
CBrush newbrush;
CBrush *oldbrush;
newbrush.CreateSolidBrush(RGB(0,0,255));
// newbrush.CreateHatchBrush(HS_CROSS,RGB(0,0,255));
// newbrush.CreateHatchBrush(HS_BDIAGONAL,RGB(0,0,255));
MapIn2();
for(i=0;i<mapH;i++)
for(j=0;j<mapV;j++)
{
if(MapTemp[j][i]==1)
{
gys=400/mapH;
x0=gys*i;
y0=gys*j;
x1=gys*i+gys;
y1=gys*j+gys;
oldbrush=dc.SelectObject(&newbrush);
dc.Rectangle(x0,y0,x1,y1);
}
}
dc.SelectObject(oldbrush);
newbrush.DeleteObject();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -