MFC按指定的数据绘制极坐标,高中的三角函数放大招呢~
Posted airduce
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC按指定的数据绘制极坐标,高中的三角函数放大招呢~相关的知识,希望对你有一定的参考价值。
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按指定的数据绘制极坐标,高中的三角函数放大招呢~的主要内容,如果未能解决你的问题,请参考以下文章