MFC-箭头的画法

来源:互联网 发布:医疗器械 淘宝 编辑:程序博客网 时间:2024/04/26 00:07
void vDrawArrow( HDC hDC, POINT startPoint, POINT EndPoint, double theta, int length ) 
{  
   
double Px = 0;
double Py = 0;
double P1x = 0;
double P1y = 0;
double P2x = 0;
double P2y = 0; 
double dVectorLen_P1 = 0;
double dVectorLen_P2 = 0;  
HBRUSH  hBrush = NULL;
HBRUSH  hOldBrush = NULL;
HPEN hPen = NULL;
HPEN hOldPen = NULL;
POINT*  pPoints = NULL;


/* < 以P2为原点得到向量P2P1(P) > */
Px = startPoint.x - endPoint.x;
Py = startPoint.y - endPoint.y;


double _len = sqrt(Px*Px + Py*Py);
if (_len==0)
{
return;
}

/* < 调整向量长度 > */

double _cos = Px/_len;
double _sin = Py/_len;
double _Px = length*_cos;
double _Py = length*_sin;

theta = PI * theta / 180;/* < 转换为弧度 > */


Px = _Px;
Py = _Py;
/* < 向量P逆时针旋转theta角得到向量P1 > */
P1x = Px * cos( theta ) - Py * sin( theta ); 
P1y = Px * sin( theta ) + Py * cos( theta ); 


/* < 向量P顺时针旋转theta角得到向量P2 > */
P2x = Px * cos( -theta ) - Py * sin( -theta );   
P2y = Px * sin( -theta ) + Py * cos( -theta );


/* < 伸缩向量至指定长度 > */
dVectorLen_P1 = sqrt( P1x * P1x + P1y * P1y );
P1x = P1x * (length / (2.0*dVectorLen_P1));     
P1y = P1y * (length / (2.0*dVectorLen_P1));  


/* < 伸缩向量至指定长度 > */
dVectorLen_P2 = sqrt( P2x * P2x + P2y * P2y );     
P2x = P2x * (length / (2.0*dVectorLen_P2));    
P2y = P2y * (length / (2.0*dVectorLen_P2));  


/* < 平移变量到直线的末端 > */ 
P1x = P1x + endPoint.x;
P1y = P1y + endPoint.y;    
P2x = P2x + endPoint.x;     
P2y = P2y + endPoint.y; 

POINT temp[3];
pPoints = temp;
pPoints[ 0 ].x = endPoint.x;
pPoints[ 0 ].y = endPoint.y;
pPoints[ 1 ].x = P1x;
pPoints[ 1 ].y = P1y;
pPoints[ 2 ].x = P2x;
pPoints[ 2 ].y = P2y;


POINT line[2];
line[0] = pPoints[0];
line[1] = pPoints[1];
int len = sqrt((double)(line[0].x-line[1].x)*(line[0].x-line[1].x)+(double)(line[0].y-line[1].y)*(line[0].y-line[1].y));
if (len>100)
{
int dd = 0;
}
::Polyline(hDC, line, 2);


line[0] = pPoints[0];
line[1] = pPoints[2];
len = sqrt((double)(line[0].x-line[1].x)*(line[0].x-line[1].x)+(double)(line[0].y-line[1].y)*(line[0].y-line[1].y));
if (len>100)
{
int dd = 0;
}
::Polyline(hDC, line, 2);


line[0].x = _Px+endPoint.x;
line[0].y = _Py+endPoint.y;
line[1] = endPoint;
if (dir!=nds::common::fixedattributes::Direction::IN_NO_DIRECTION)
{
len = sqrt((double)(line[0].x-line[1].x)*(line[0].x-line[1].x)+(double)(line[0].y-line[1].y)*(line[0].y-line[1].y));
if (len>100)
{
int dd = 0;
}
::Polyline(hDC, line, 2);
}
//DrawOneName(hDC, line[0].x, line[0].y, _name, MAX_PATH);


SelectObject( hDC, hOldPen );
::DeleteObject( hPen );
//SelectObject( hDC, hOldBrush );
//::DeleteObject( hBrush );

}
原创粉丝点击