www.pudn.com > RADAR-ALARM.rar > DrawWindSingal.cpp, change:2011-10-18,size:5238b


#include "StdAfx.h" 
#include "DrawWindSingal.h" 
#include <math.h> 
#define PI 3.1415926//pi 
#define shortLineLenth 6//风速短线长度 
#define longLineLenth 12//风速长线长度 
#define SignalLineLenth 30//风速标志杆长度 
#define lineInterval 3//风速表示线段之间间隔,三角为间隔的两倍 
CDrawWindSingal::CDrawWindSingal(void) 
{ 
} 
 
 
CDrawWindSingal::~CDrawWindSingal(void) 
{ 
} 
 
void CDrawWindSingal::GetLineNum(int windVel, int& longLineNum, int& shortLineNum,int& triangleNum) 
{ 
	__try 
	{ 
		int value=windVel+1; 
		if(windVel==1) 
		{ 
			longLineNum=0; 
			shortLineNum=0; 
			triangleNum=0; 
		} 
		else if(windVel>=2) 
		{ 
			longLineNum=value/4; 
			shortLineNum=value%4/2; 
			if(longLineNum>=5) 
			{ 
				triangleNum=longLineNum/5; 
				longLineNum=longLineNum%5; 
			} 
			else 
				triangleNum=0; 
 
			     
		} 
		 
 
	} 
	__except( EXCEPTION_EXECUTE_HANDLER )  
	{ 
		longLineNum=0; 
		shortLineNum=0; 
		triangleNum=0; 
	} 
} 
 
 
void CDrawWindSingal::DrawTriangle(CPoint point, int lenth,float angle, CDC* pDC, COLORREF color) 
{ 
	CPen pen; 
	CPen *oldpen; 
	CPoint pt2,pt3; 
	int x,y; 
	calPointBaseONCoodinate(point,lenth,angle,x,y); 
	pt2.SetPoint(x,y); 
	angle=angle+90; 
	if(angle>=360) angle-=360; 
	pt3.SetPoint((pt2.x-point.x)/2+point.x,(pt2.y-point.y)/2+point.y); 
	calPointBaseONCoodinate(pt3,lenth*1.5,angle,x,y); 
	pt3.SetPoint(x,y); 
	pen.CreatePen(PS_SOLID,1,color); 
	oldpen=pDC->SelectObject(&pen); 
	pDC->MoveTo(point); 
	pDC->LineTo(pt3); 
	pDC->MoveTo(pt3); 
	pDC->LineTo(pt2); 
	pDC->MoveTo(point); 
	pDC->LineTo(pt2); 
	pDC->SelectObject(oldpen); 
	DeleteObject(pen); 
	DeleteObject(*oldpen); 
 
} 
 
 
void CDrawWindSingal::calPointBaseONCoodinate(CPoint point, int lenth, float angle, int& x, int& y) 
{ 
	__try 
	{ 
		if(angle<90&&angle>=0) 
		{ 
			x=point.x+(int)(lenth*sin(angle/180.0*PI)); 
			y=point.y-(int)(lenth*cos(angle/180.0*PI)); 
		} 
		else if(angle<180&&angle>=90) 
		{ 
			x=point.x+(int)(lenth*cos((angle-90)/180.0*PI)); 
			y=point.y+(int)(lenth*sin((angle-90)/180.0*PI)); 
		} 
		else if(angle<270&&angle>=180) 
		{ 
			x=point.x-(int)(lenth*sin((angle-180)/180.0*PI)); 
			y=point.y+(int)(lenth*cos((angle-180)/180.0*PI)); 
		} 
		else if(angle<360&&angle>=270) 
		{ 
			x=point.x-(int)(lenth*cos((angle-270)/180.0*PI)); 
			y=point.y-(int)(lenth*sin((angle-270)/180.0*PI)); 
		} 
	} 
	__except( EXCEPTION_EXECUTE_HANDLER )  
	{ 
		x=0; 
		y=0; 
	} 
} 
 
 
void CDrawWindSingal::DrawWindSingnal(CPoint pt, int velWind, float angle, COLORREF color,CDC*pDC) 
{ 
	int lenth; 
 
	int longLineNum,shortLineNum,triangleNum; 
	GetLineNum(velWind,longLineNum,shortLineNum,triangleNum); 
	if((longLineNum+triangleNum*2)*lineInterval<(SignalLineLenth/2)) lenth=SignalLineLenth ; 
	else lenth=SignalLineLenth/2+(longLineNum+shortLineNum+triangleNum*2)*lineInterval; 
	if (velWind==0) return; 
	else if (velWind==1)  
	{ 
		DrawSolidPoint(pt,pDC,color); 
		DrawLineBaseOnCoordinate(pt,SignalLineLenth,angle,pDC,color); 
	} 
	else if (velWind==2) 
	{ 
		float tempangle=angle+90; 
		if(tempangle>=360) tempangle=tempangle-360; 
		DrawSolidPoint(pt,pDC,color); 
		DrawLineBaseOnCoordinate(pt,SignalLineLenth,angle,pDC,color); 
		int x,y; 
		calPointBaseONCoodinate(pt,lenth-2*lineInterval,angle,x,y); 
		DrawLineBaseOnCoordinate( CPoint(x,y), shortLineLenth, tempangle,pDC,color); 
	} 
 
	else  
	{ 
		DrawSolidPoint(pt,pDC,color); 
		DrawLineBaseOnCoordinate(pt,lenth,angle,pDC,color); 
		for(int i=1;i<=triangleNum;i++) 
		{ 
			int x,y; 
			calPointBaseONCoodinate(pt,lenth-i*lineInterval*2,angle,x,y); 
			DrawTriangle(CPoint(x,y), lineInterval*2,angle, pDC, color); 
		} 
		for(int i=0;i<longLineNum;i++) 
		{ 
			int x,y; 
			calPointBaseONCoodinate(pt,lenth-triangleNum*2*lineInterval-i*lineInterval,angle,x,y); 
			float tempangle=angle+90; 
			if(tempangle>=360) tempangle=tempangle-360; 
			DrawLineBaseOnCoordinate( CPoint(x,y), longLineLenth , tempangle,pDC,color); 
 
		} 
		for(int i=0;i<shortLineNum;i++) 
		{ 
			int x,y; 
			calPointBaseONCoodinate(pt,lenth-triangleNum*2*lineInterval-longLineNum*lineInterval-i*lineInterval,angle,x,y); 
			float tempangle=angle+90; 
			if(tempangle>=360) tempangle=tempangle-360; 
			DrawLineBaseOnCoordinate( CPoint(x,y), shortLineLenth, tempangle,pDC,color); 
		} 
	} 
} 
 
 
void CDrawWindSingal::DrawSolidPoint(CPoint pt, CDC* pDC , COLORREF color) 
{ 
	     CBrush *oldbrush; 
		 CPen *oldpen; 
	     CBrush brush(color); 
		 oldbrush=pDC->SelectObject(&brush); 
		 CPen pen; 
		 pen.CreatePen(PS_SOLID,2,color); 
		 oldpen=pDC->SelectObject(&pen); 
		 pDC->Ellipse(pt.x-2,pt.y-2,pt.x+2,pt.y+2); 
		 pDC->SelectObject(oldpen); 
		 pDC->SelectObject(oldbrush); 
		 DeleteObject(pen); 
		 DeleteObject(brush); 
		 DeleteObject(*oldbrush); 
		 DeleteObject(*oldpen); 
 
} 
 
// 基于极坐标画线,以北方为0度,顺时针方向增加 
void CDrawWindSingal::DrawLineBaseOnCoordinate(CPoint point, int lenth, float angle,CDC*pDC,COLORREF color) 
{ 
	int x,y; 
	calPointBaseONCoodinate(point,lenth,angle,x,y); 
	CPen pen; 
	CPen *oldpen; 
	pen.CreatePen(PS_SOLID,1,color); 
	oldpen=pDC->SelectObject(&pen); 
	pDC->MoveTo(point); 
	pDC->LineTo(CPoint(x,y)); 
	pDC->SelectObject(oldpen); 
	DeleteObject(pen); 
	DeleteObject(*oldpen); 
	 
	 
	 
}