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

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

通过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: 在此处为本机数据添加绘制代码
}

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

若文章对您有帮助,帮忙点个赞!

0
-3
发布时间 2019-06-01 09:33:52
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)