64,654
社区成员
发帖
与我相关
我的任务
分享
void CSin3View::OnDraw(CDC* pDC)
{
CSin3Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rc;
GetClientRect(&rc);
CPen *oldPen;
CPen newPen(PS_SOLID, 1, RGB(0,0,0));
oldPen = pDC->GetCurrentPen();
pDC->SelectObject(&newPen);
int clientWidth = rc.Width();
int clientHeight = rc.Height();
CPoint pCenter; // 坐标系原点
//pCenter.x = long(clientWidth * 0.4);
pCenter.x = 300;
pCenter.y = long(clientHeight / 2);
//画X轴
// pDC->MoveTo(pCenter.x / 4 - 40, pCenter.y);
// pDC->LineTo(clientWidth - pCenter.x / 4 + 40, pCenter.y);
pDC->MoveTo(50, pCenter.y);
pDC->LineTo(760, pCenter.y);
//画Y轴
pDC->MoveTo(pCenter.x, int(pCenter.y / 4));
pDC->LineTo(pCenter.x, int(pCenter.y * 1.6));
//画Sin(X)
const double pi = 3.14;
//int pstart = int(pCenter.x / 4); // 曲线第一个点横坐标
//int pend = int(clientWidth - pCenter.x / 4); // 曲线最后一个点横坐标
int pstart = 100; // 曲线第一个点横坐标
int pend = 700; // 曲线最后一个点横坐标
int y = int(pCenter.y - sin((pstart - pCenter.x) / 180. * pi) * 100); // 求第一点的纵坐标
pDC->MoveTo(pstart, y); // 鼠标移动到第一点处
for (int i = pstart + 1; i <= pend; i++) // 画接下来的点
{
y = int(pCenter.y - sin((i - pCenter.x) / 180. * pi) * 100); // 求每个点的纵坐标
pDC->LineTo(i, y);
}
pDC->SelectObject(oldPen);
}
CRect rc;
GetClientRect(&rc);
CPen *oldPen;
CPen newPen(PS_SOLID,1,RGB(0,0,0));
oldPen = pDC->GetCurrentPen();
pDC->SelectObject(&newPen);
int clientWidth = rc.Width();
int clientHeight = rc.Height();
//画X轴
pDC->MoveTo(0,clientHeight/2);
pDC->LineTo(clientWidth,clientHeight/2);
//画Y轴
pDC->MoveTo(clientWidth/2,0);
pDC->LineTo(clientWidth/2,clientHeight);
//画Sin(X)
pDC->MoveTo(0,clientHeight/2);
for (int i = 0; i < clientWidth; i++)
{
pDC->LineTo(i,sin((double)i) * 10 + clientHeight/2);
}
pDC->SelectObject(oldPen);