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

📄 draw.cpp

📁 实现了无线传感器网络中的一个路由协议算法。
💻 CPP
字号:
// Draw.cpp: implementation of the CDraw class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MainFrm.h"
#include "SimSensorView.h"
#include "MsgCenter.h"
#include "SimSensor.h"
#include "Draw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


extern CMsgCenter MsgCenter;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDraw::CDraw()
{ 
	//InitializeCriticalSection(&Locked);
}

CDraw::~CDraw()
{
	//DeleteCriticalSection(&Locked);
}

void CDraw::DrawCoordinates(CDC *pDC)
{
	CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	CBrush *pOldBrush=pDC->SelectObject(pBrush);
    CPoint x,y;
	CString Str;
	double Radius;

	for (int i=1;i<=BONDERY;i++) {
		Radius = (float)i * RADIS;
		Str.Format("%d",i);
		x.x = (long)(g_ViewCenterX - RATOS*Radius); 
		x.y = (long)(g_ViewCenterY - RATOS*Radius);
		y.x = (long)(g_ViewCenterX + RATOS*Radius);
		y.y = (long)(g_ViewCenterY + RATOS*Radius);
		pDC->Ellipse(CRect(x,y));
		pDC->TextOut((long)(g_ViewCenterX+3*RATOS*RADIS/4+(i-1)*RATOS*RADIS),(long)(g_ViewCenterY+2),Str);
	}
	
	for (i=1;i<=8;i++) {
        double Angle;
		Angle = (float)(i-1) * ( PAI/4 );
		Str.Format("%d",i);
		x.x = (long)g_ViewCenterX;
		x.y = (long)g_ViewCenterY;
		y.x = (long)(g_ViewCenterX + g_ViewCenterX / cos(PAI / 4) * cos(Angle));
		y.y = (long)(g_ViewCenterY - g_ViewCenterX / cos(PAI / 4) * sin(Angle));
		pDC->MoveTo(x);
		pDC->LineTo(y);	
		pDC->TextOut((long)(g_ViewCenterX+RATOS*Radius*cos(Angle+PAI/64)),(long)(g_ViewCenterY-RATOS*Radius*sin(Angle+PAI/64)),Str);
	}

	pDC->SelectObject(pOldBrush);
}

//DEL void CDraw::DrawAllNodes()
//DEL {
//DEL 	while (MsgCenter.Locked[0][1]==true) Sleep(1);
//DEL     MsgCenter.Locked[0][1]=true;
//DEL 
//DEL 	CNodeStrain *pStrain = MsgCenter.pNodeStrain[0][1];
//DEL 	while (pStrain!=NULL) {
//DEL         pStrain->pNode->Draw();
//DEL 		pStrain = pStrain->pNext;
//DEL 	}
//DEL 
//DEL 	MsgCenter.Locked[0][1]=false;
//DEL }

void CDraw::DrawAllNodes(CView *pView,CDC *pDC)
{

    CBrush *pNullBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	CBrush *pGrayBrush=CBrush::FromHandle((HBRUSH)GetStockObject(GRAY_BRUSH));
	
   	CBrush *pOldBrush;
  
    CPoint x,y;
	CString Str;
	double NodeCenterX,NodeCenterY;

	CPen GrayPen(0,0,RGB(128,128,128));
	CPen RedPen(0,0,RGB(255,0,0));
	CPen BluePen(0,0,RGB(0,0,255));
	CPen WhitePen(0,0,RGB(255,255,255));
    CPen *pOldPen;

	CFont font;
    font.CreatePointFont(70,"宋体",pDC);

	if (NULL == MsgCenter.pNodeStrain) return;

	EnterCriticalSection(&(MsgCenter.Locked[0][1]));
    
	CFont *pOldFont=pDC->SelectObject(&font);
	CNodeStrain *pStrain = MsgCenter.pNodeStrain[0][1];
	CNodeStrain *pSinkStrain = MsgCenter.pNodeStrain[0][0];

    if ((pStrain==NULL) && (pSinkStrain!=NULL) ) {
		//CSimSensorView *pView = (CSimSensorView *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveView();
		pView->Invalidate ();
	}
	
	while (pStrain!=NULL) {
		NodeCenterX = g_ViewCenterX + RATOS*(pStrain->pNode->Distance)*cos((pStrain->pNode->Angle));
		NodeCenterY = g_ViewCenterY - RATOS*(pStrain->pNode->Distance)*sin((pStrain->pNode->Angle));		
		x.x = (long)(NodeCenterX - 4); 
		x.y = (long)(NodeCenterY - 4);
		y.x = (long)(NodeCenterX + 4);
		y.y = (long)(NodeCenterY + 4);

        EnterCriticalSection(&(pStrain->pNode->Locked));

        //Str.Format("E:%d,S:%d", (int)(pStrain->pNode->RemainEnergy),pStrain->pNode->Status);
		Str.Format("%0.1f",(pStrain->pNode->RemainEnergy));
		pOldPen = pDC->SelectObject(&WhitePen);	
        pDC->Rectangle(x.x,x.y,(y.x+26),y.y);
        pDC->SelectObject(pOldPen);	
		
		
        if ( (pStrain->pNode->RemainEnergy) > 0 ) {
			switch ( (pStrain->pNode->Status) ) {
			case 0:	
                pOldPen = pDC->SelectObject(&GrayPen);
				pOldBrush=pDC->SelectObject(pGrayBrush); 
				pDC->Ellipse(CRect(x,y));
				pDC->TextOut(x.x+9,x.y,Str);
				pDC->SelectObject(pOldBrush);
				pDC->SelectObject(pOldPen);	
				break;
			case 1:
				pOldPen = pDC->SelectObject(&RedPen);	
				pDC->Ellipse(CRect(x,y));
				pDC->TextOut(x.x+9,x.y,Str);
				pDC->SelectObject(pOldPen);	
				break;
			case 2:
				pOldPen = pDC->SelectObject(&BluePen);	
				pDC->Ellipse(CRect(x,y));
				pDC->TextOut(x.x+9,x.y,Str);
				pDC->SelectObject(pOldPen);	
				break;
			default:;	
			}
		}
		
        LeaveCriticalSection(&(pStrain->pNode->Locked)); 

		pStrain = pStrain->pNext;
	}

    pDC->SelectObject(pOldFont);

	LeaveCriticalSection(&(MsgCenter.Locked[0][1]));
}

void CDraw::DrawGraph(CView *pView, CDC *pDC)
{  
 	CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	CBrush *pOldBrush=pDC->SelectObject(pBrush);
    CPoint x,y;
	CString Str;

	GraphX = g_GraphViewCenterX - GRAPH_WIDTH / 2;
    GraphY = 156;
	x.x = (long)GraphX;
	x.y = (long)GraphY;
	y.x = (long)(GraphX + GRAPH_WIDTH);
	y.y = (long)GraphY;
	pDC->MoveTo(x);
	pDC->LineTo(y);	

	x.x = (long)(GraphX + GRAPH_WIDTH);
	x.y = (long)GraphY;
	y.x = x.x - 5;

	y.y = (long)GraphY - 5;
	pDC->MoveTo(x);
	pDC->LineTo(y);	

	y.y = (long)GraphY + 5;
	pDC->MoveTo(x);
	pDC->LineTo(y);	
    //************************

	x.x = (long)GraphX;
	x.y = (long)GraphY;
	y.x = (long)GraphX;
	y.y = (long)(GraphY - GRAPH_HEIGHT);
	pDC->MoveTo(x);
	pDC->LineTo(y);	

	x.x = (long)GraphX;
	x.y = (long)(GraphY - GRAPH_HEIGHT);
	y.x = x.x - 5;
	y.y = x.y + 5;
	pDC->MoveTo(x);
	pDC->LineTo(y);	

	y.x = x.x + 5;
	y.y = x.y + 5;
	pDC->MoveTo(x);
	pDC->LineTo(y);	


    if (NULL == MsgCenter.pNodeStrain) {
		pDC->SelectObject(pOldBrush);
		return;
	}
 	double TotalEnergy;
	int Num;

	for (int i=1;i<=BONDERY;i++) {
		TotalEnergy=0;
		Num = 0;
		for (int j=1;j<=8;j++) {
            EnterCriticalSection(&(MsgCenter.Locked[i][j]));
            CNodeStrain *pStrain = MsgCenter.pNodeStrain[i][j]; 
	        while (pStrain!=NULL) {
                   EnterCriticalSection(&(pStrain->pNode->Locked));
                   TotalEnergy = TotalEnergy + (pStrain->pNode->RemainEnergy);
				   Num ++;
                   LeaveCriticalSection(&(pStrain->pNode->Locked)); 
		           pStrain = pStrain->pNext;
			}
			LeaveCriticalSection(&(MsgCenter.Locked[i][j]));
		}
		double CenterX;
		if ( Num ) {
			CenterX = GraphX + (2*i - 1) * GRAPH_WIDTH / BONDERY / 2;
			x.x = (long)(CenterX - 10);
			x.y = (long)(GraphY - (GRAPH_HEIGHT - (GRAPH_HEIGHT / g_NodeEnergy) * TotalEnergy / Num));
			if (x.y > (long)GraphY) x.y = (long)GraphY;
			y.x = (long)(CenterX + 10);
			y.y = (long)(GraphY);
			pDC->Rectangle (CRect(x,y));
			Str.Format("%d",i);
		    pDC->TextOut ((long)CenterX,(long)GraphY + 2,Str);
        }
		
 
	}
	pDC->SelectObject(pOldBrush);
}

void CDraw::DrawListView(int NodeId,BOOL Type, LPVOID *pParam)
{
	//static long i=0;
	CSimSensorDoc *pDoc = (CSimSensorDoc *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveView()->GetDocument();
	CString strMsg,strId;
    CMsg *pMsg;

	EnterCriticalSection(&(pDoc->pMsgListView->Locked));
	CListCtrl &ListCtrl =pDoc->pMsgListView->GetListCtrl();
	pMsg = (CMsg *)pParam;
	switch ( NodeId ) {
	case 0:
		switch ( Type ) {
		case DEAD:
			strMsg.Format("Sink Dead!"); 
			break;
		case SEND:
			strMsg.Format("Sink Send Msg:((%d,%d),(%d,%d),%d,data)",pMsg->Destination.x,pMsg->Destination.y,pMsg->Origination.x,pMsg->Origination.y,pMsg->Type); 
		    break;
		case RECEIVE:
			strMsg.Format("Sink Received Msg:((%d,%d),(%d,%d),%d,data)",pMsg->Destination.x,pMsg->Destination.y,pMsg->Origination.x,pMsg->Origination.y,pMsg->Type); 
		    break;
		case RELAY:
			strMsg.Format("Sink Relay Msg:((%d,%d),(%d,%d),%d,data)",pMsg->Destination.x,pMsg->Destination.y,pMsg->Origination.x,pMsg->Origination.y,pMsg->Type); 
		}
		break;
		default:
			switch ( Type ) {
			case DEAD:
				strMsg.Format("Node %d Dead!",NodeId); 
				break;
			case SEND:
				strMsg.Format("Node %d Send Msg:((%d,%d),(%d,%d),%d,data)",NodeId,pMsg->Destination.x,pMsg->Destination.y,pMsg->Origination.x,pMsg->Origination.y,pMsg->Type);     
			    break;
			case RECEIVE:
				strMsg.Format("Node %d Received Msg:((%d,%d),(%d,%d),%d,data)",NodeId,pMsg->Destination.x,pMsg->Destination.y,pMsg->Origination.x,pMsg->Origination.y,pMsg->Type);     
			    break;
			case RELAY:
				strMsg.Format("Node %d Relay Msg:((%d,%d),(%d,%d),%d,data)",NodeId,pMsg->Destination.x,pMsg->Destination.y,pMsg->Origination.x,pMsg->Origination.y,pMsg->Type);     
			}
	}
	
	//strId.Format("%d",i++);
	//ListCtrl.InsertItem (0,strId,0);
	//ListCtrl.SetItemText(0,1,strMsg);
	ListCtrl.InsertItem (0,strMsg,0);
	LeaveCriticalSection(&(pDoc->pMsgListView->Locked));
}

⌨️ 快捷键说明

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