C++开发绘制正弦曲线的方法

本文实例为大家分享了C++绘制正弦曲线的具体代码,供大家参考,具体内容如下

C++开发绘制正弦曲线的方法_第1张图片

通过OnDraw函数绘制图形

过程为先绘制两个坐标轴,再绘制正弦曲线的方法。其中将曲线分解为660个像素点,让其看起来更像“曲线”

void CMFCApplication3View::OnDraw(CDC* pDC)
{
 CMFCApplication3Doc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 if (!pDoc)
 return;
 CPen cpen, pen;
 pen.CreatePen(PS_DASH, 4, RGB(0, 0, 0));  //建立画笔
 cpen.CreatePen(PS_DASH, 2, RGB(0, 0, 255));
 pDC->SelectObject(&cpen);      
 pDC->SetViewportOrg(200, 345);  //指定原点
 pDC->SetTextColor(RGB(255, 0, 0));
 CString sPITEX[] = { "-1/2π","","1/2π","π","3/2π","2π","5/2π","3π","7/2π","4π","9/2π","5π" };
 for (int n = -1, nTmp = 0; nTmp < 660; n++, nTmp += 60) {
 pDC->LineTo(60 * n, 0);
 pDC->LineTo(60 * n, -5);
 pDC->MoveTo(60 * n, 0);
 pDC->TextOutA(60 * n - sPITEX[n + 1].GetLength() * 3, 16, sPITEX[n + 1]);
 }
 pDC->MoveTo(0, 0);
 CString sTmp;
 for (int n = -4, nTmp = 0; nTmp <= 180; n++, nTmp = 60 * n) {
 pDC->LineTo(0, 60 * n);
 pDC->LineTo(5, 60 * n);
 pDC->MoveTo(0, 60 * n);
 sTmp.Format("%d", -n);
 pDC->TextOutA(10, 60 * n, sTmp);
 }
 double y, radian;
 pDC->SelectObject(&pen);
 for (int x = -60; x < 600; x++) {
 radian = x / ((double)60 * 2)*3.1415;
 y = sin(radian) * 2 * 60;
 pDC->MoveTo((int)x, (int)y);
 pDC->LineTo((int)x, (int)y);
 }
 cpen.DeleteObject();
 pen.DeleteObject();
 // TODO: 在此处为本机数据添加绘制代码
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(C++开发绘制正弦曲线的方法)