码迷,mamicode.com
首页 > 编程语言 > 详细

MFC按指定的数据绘制极坐标,高中的三角函数放大招呢~

时间:2018-10-18 13:08:31      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:ntp   pen   creat   step   ***   img   位置   lin   color   

void CDrawPoleCoordinateDlg::OnBnClickedButton1()
{
    /**************************可调整的数值******************************/
    float angle = 30;//每个扇形的角度
    int radius = 300;//半径
    int step = 30;//每一环的相隔距离
    CPoint center(radius+200,radius+50);//圆心
    int radiusScale = 100;//半径的刻度
    /*********************************************************************/
    

    /*************************测试数据*********************************/
    std::list<PoparValue> list ;

    PoparValue p(40,27);
    list.push_back(p);

    PoparValue p2(70,60);
    list.push_back(p2);

    PoparValue p3(80,90);
    list.push_back(p3);

    PoparValue p4(66,200);
    list.push_back(p4);

    PoparValue p5(30,255);
    list.push_back(p5);
    /*************************测试数据*********************************/

    CDC *pDC = GetDC();
    
    //绘制同心圆
    int myRadius = 0;
    int num = 0;
    for(int i = 0;i<radius;i+=step){
        myRadius = radius - i;
        if(num%2==0){
            CPen myPen5;
            myPen5.CreatePen(PS_SOLID,1,RGB(180,180,180));
            pDC->SelectObject(myPen5);
            pDC->Ellipse(center.x-myRadius,center.y-myRadius,center.x+myRadius,center.y+myRadius);
        }else{
            CPen myPen6;
            myPen6.CreatePen(PS_DOT,1,RGB(220,220,220));
            pDC->SelectObject(myPen6);
            pDC->Ellipse(center.x-myRadius,center.y-myRadius,center.x+myRadius,center.y+myRadius);
        }
        if(i==0){
            CPen myPen5;
            myPen5.CreatePen(PS_SOLID,1,RGB(0,0,0));
            pDC->SelectObject(myPen5);
            pDC->Ellipse(center.x-myRadius,center.y-myRadius,center.x+myRadius,center.y+myRadius);
        }
        
        num++;
    }

    CPen myPen2;
    myPen2.CreatePen(PS_SOLID,1,RGB(180,180,180));
    pDC->SelectObject(myPen2);

    //绘制分格
    for(float i = 0;i<360;){
        pDC->MoveTo(center);
        float x = (float)(radius*cos(i*PI/180))+center.x;
        float y = (float)(radius*sin(i*PI/180))+center.y;
        pDC->LineTo(CPoint((int)x,(int)y));
        //调整度数的位置
        CString str;
        str.Format(_T("%0.0f"),i);
        str = str + L"°";
        if(i>=0 && i<=45)
            pDC->TextOutW(x,y,str);
        else if(i>45 && i<=90)
            pDC->TextOutW(x-(5+sin(i*PI/180)),y,str);
        else if(i>90 && i<=135)
            pDC->TextOutW(x-(35-sin(i*PI/180)),y+(4-sin(i*PI/180)),str);
        else if(i>135 && i<=180)
            pDC->TextOutW(x-(40-sin(i*PI/180)),y,str);
        else if(i>180 && i<=225)
            pDC->TextOutW(x-(40-sin(i*PI/180)),y-(10-sin(i*PI/180)),str);
        else if(i>225 && i<=270)
            pDC->TextOutW(x-(20+sin(i*PI/80)),y-(20+cos(i*PI/180)),str);
        else if(i>270 && i<=315)
            pDC->TextOutW(x-(5+cos(i*PI/180)),y-(15+cos(i*PI/180)),str);
        else if(i>315 && i<=360)
            pDC->TextOutW(x+(5+sin(i*PI/180)),y-(10+sin(i*PI/180)),str);
        i +=angle;
        
    }
    
    

    CString CRadiusScale;
    CRadiusScale.Format(_T("%d"),radiusScale);
    //显示半径刻度
    pDC->TextOutW(center.x-radius-50,center.y-radius+90,L"半径长度值: "+CRadiusScale);

    //每环相隔距离
    CString CStep;
    CStep.Format(_T("%d"),step);
    pDC->TextOutW(center.x-radius-50,center.y-radius+40,L"每环相隔距离: "+CStep);

    //显示圆心位置
    CString Cx;
    Cx.Format(_T("%ld"),center.x);

    CString Cy;
    Cy.Format(_T("%ld"),center.y);
    pDC->TextOutW(center.x-radius-50,center.y-radius-10,L"圆心:("+Cx+L","+Cy+L"");
    
    CString CAangle;
    CAangle.Format(_T("%0.0f"),angle);
    //扇形角度
    pDC->TextOutW(center.x-radius-50,center.y-radius+65,L"扇形角度: "+CAangle);

    //显示半径
    CString CRadius;
    CRadius.Format(_T("%d"),radius);
    pDC->TextOutW(center.x-radius-50,center.y-radius+15,L"半径: "+CRadius);

    std::list<PoparValue>::iterator ite;

    CPen myPen3;
    myPen3.CreatePen(PS_SOLID,2,RGB(255,0,0));
    pDC->SelectObject(myPen3);

    //绘制点
    for(ite = list.begin();ite!=list.end();ite++){
        Point myPoint = getPointPosition(ite->ang*radius/radiusScale,ite->val,center);
        pDC->Ellipse(int(myPoint.x-2),int(myPoint.y-2),int(myPoint.x+2),int(myPoint.y+2));
    }
    std::list<PoparValue>::iterator current = list.begin();
    std::list<PoparValue>::iterator previous = list.begin();


    //连线

    CPen myPen4;
    myPen4.CreatePen(PS_SOLID,1,RGB(255,97,10));
    pDC->SelectObject(myPen4);

    while(current!=list.end()){
        Point bPoint = getPointPosition(previous->ang*radius/radiusScale,previous->val,center);
        CPoint begin;
        begin.x = bPoint.x;
        begin.y = bPoint.y;
        pDC->MoveTo(begin);

        current++;
        if(current!=list.end()){
            Point bPoint = getPointPosition(current->ang*radius/radiusScale,current->val,center);
            CPoint begin;
            begin.x = bPoint.x;
            begin.y = bPoint.y;
            pDC->LineTo(begin);
        }
        previous = current;
    }


    ReleaseDC(pDC);
}

绘制效果:
  技术分享图片

MFC按指定的数据绘制极坐标,高中的三角函数放大招呢~

标签:ntp   pen   creat   step   ***   img   位置   lin   color   

原文地址:https://www.cnblogs.com/airduce/p/9809766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!