项目名:XOY2
void CXOY2View::OnDraw(CDC* pDC)
{
CXOY2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CRect rectClient;
GetClientRect(rectClient);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(1000,1000);
pDC->SetViewportExt(rectClient.right,-rectClient.bottom);
pDC->SetViewportOrg(rectClient.right/2,rectClient.bottom/2);
pDC->MoveTo(-500,0);
pDC->LineTo(500,0);//绘制X轴
pDC->LineTo(490,10);
pDC->MoveTo(500,0);
pDC->LineTo(490,-10);//绘制x轴箭头
for(int i=-5;i<=5;i++)
{
pDC->MoveTo(100*i,-5);
pDC->LineTo(100*i,5);
CString Str;
Str.Format(_T("%d"),i);
if(i!=0)
{
pDC->TextOut(100*i-5,-10,Str);
}
}//绘制X轴坐标尺
pDC->MoveTo(0,-500);
pDC->LineTo(0,500);//绘制Y轴
pDC->LineTo(-10,490);
pDC->MoveTo(0,500);
pDC->LineTo(10,490);//绘制y轴箭头
for(int i=-5;i<=5;i++)
{
pDC->MoveTo(-5,100*i);
pDC->LineTo(5,100*i);
CString Str;
Str.Format(_T("%d"),i);
pDC->TextOut(-20,100*i-5,Str);
}//绘制Y轴坐标尺
pDC->TextOut(490,-40,_T("X"));
pDC->TextOut(-40,490,_T("Y"));
CPen pen_Zuobixi,pen_sinx;
pen_Zuobixi.CreatePen(PS_SOLID,4,RGB(0,0,0));
pen_sinx.CreatePen(PS_SOLID,2,RGB(0,0,255));
pDC->SelectObject(&pen_Zuobixi);
double y,radian;
pDC->SelectObject(&pen_sinx);
for(int x=-500;x<500;x++)
{
//弧度=X坐标/曲线宽度*角系数*π
//Y坐标=振幅*曲线宽度*sin(弧度)
radian =x/((double)314.159)*3.14159;
y=sin(radian)*100;
pDC->MoveTo((int)x,(int)y);
pDC->LineTo((int)x,(int)y);
}
pen_sinx.DeleteObject();
pen_Zuobixi.DeleteObject();
}
运行结果如下: