C++开发绘制正弦曲线的方法
时间:2020-12-09 20:05:53|栏目: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: 在此处为本机数据添加绘制代码 }
上一篇:如何统计在一篇文章中某个单词出现了几次,以及第一次出现的位置
栏 目:C代码
下一篇:DHCP:解析开发板上动态获取ip的2种实现方法详解
本文标题:C++开发绘制正弦曲线的方法
本文地址:http://www.codeinn.net/misctech/30843.html