用鼠标画五角星

来源:互联网 发布:软件质量度量指标 编辑:程序博客网 时间:2024/04/29 01:18
int g____times = 0;double g____x1, g____y1, g____x2, g____y2;#definePI(3.14159)void drawfive( CClientDC *dc ){dc->Ellipse( g____x2 - 2, g____y2 - 2, g____x2 + 2, g____y2 + 2 );// p1, p2的中间点double tx1, ty1;tx1 = ( g____x1 + g____x2 ) / 2;ty1 = ( g____y1 + g____y2 ) / 2;//dc->Ellipse( tx1 - 2, ty1 - 2, tx1 + 2, ty1 + 2 );//dc->MoveTo( g____x1, g____y1 );//dc->LineTo( g____x2, g____y2 );// 计算p1, p2的角度double angle;angle = ( g____y2 - g____y1 ) / ( g____x2 - g____x1 );if( angle > 1000000.0 ){angle = PI / 2 ;}else{angle = atan( angle );}angle += PI / 2;double g____cx, g____cy;double e;// 中间点到圆心的距离double t;// p1, p2两点的距离的一半t = sqrt( ( g____y2 - g____y1 ) * ( g____y2 - g____y1 ) + ( g____x2 - g____x1 ) * ( g____x2 - g____x1 ) );t /= 2.0;e = t * 1.3763819204711735382072095819109;// 画出圆心g____cx = tx1 + e * cos( angle );g____cy = ty1 + e * sin( angle );dc->Ellipse( g____cx - 2, g____cy - 2, g____cx + 2, g____cy + 2 );// 计算第一个点到圆心的角度angle = atan( ( g____y1 - g____cy ) / ( g____x1 - g____cx ) );angle += ( PI / 5 );// 计算5个点的坐标double x[ 5 ], y[ 5 ];double r;r = sqrt( ( g____y2 - g____cy ) * ( g____y2 - g____cy ) + ( g____x2 - g____cx ) * ( g____x2 - g____cx ) );int i;for( i = 0; i < 5; i++ ){x[ i ] = g____cx + r * cos( angle );y[ i ] = g____cy + r * sin( angle );angle += ( PI * 2 / 5 );dc->Ellipse( x[ i ] - 2, y[ i ] - 2, x[ i ] + 2, y[ i ] + 2 );}int j;for( i = 0; i < 5; i++ ){j = ( i + 2 ) % 5;dc->MoveTo( x[ i ], y[ i ] );dc->LineTo( x[ j ], y[ j ] );}}void CTestFiveDlg::OnLButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultif( g____times % 2 ){g____x2 = point.x;g____y2 = point.y;CClientDC dc( this );drawfive( &dc );}else{g____x1 = point.x;g____y1 = point.y;CClientDC dc( this );dc.Ellipse( g____x1 - 2, g____y1 - 2, g____x1 + 2, g____y1 + 2 );}g____times++;CDialog::OnLButtonUp(nFlags, point);}


 

原创粉丝点击