高等数学函数图像绘制

学数学的时候看到书上的函数绘图 于是用MFC和GDI+ 实现了一个简单绘图程序 

MFC 还是那么难用
GDI+ 就是为了抗锯齿

另外绘图的时候x轴与y轴的比例是不一样 真实比例画出来的图是很不和谐的

https://download.csdn.net/download/geforceno1/11149948

void CmyMathView::OnDraw(CDC* pDC)
{
    CmyMathDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    Graphics graphics(pDC->m_hDC);

    Gdiplus::Pen pen(Color(255, 0, 0, 255), 1);

    RECT rect;
    GetClientRect(&rect);
    int w = rect.right;
    int h = rect.bottom;
    int w2 = rect.right / 2;
    int h2 = rect.bottom / 2;

    graphics.SetSmoothingMode(SmoothingModeHighQuality);

    CList<Gdiplus::Point> srcPoints;

    for (float x = -w2; x < w2; x+=0.1f)
    {
        float y = x;
        
        switch (m_fType)
        {
        case 0:    y = x; break;
        case 1: y = pow(x, 2); break;
        case 2: y = 1 + 1 / x; break;
        case 3: y = 1 / x; break;
        case 4: y = sin(x); break;
        case 5: y = cos(x); break;
        }

        if (m_fType == 0)
            srcPoints.AddTail(Gdiplus::Point(x + w2, (h2 - y)));
        else if (m_fType == 2)
            srcPoints.AddTail(Gdiplus::Point(x * 20 + w2, (h2 - y * 80)));
        else
            srcPoints.AddTail(Gdiplus::Point(x * 50 + w2, (h2 - y * 100)));
    }

    Gdiplus::Point * points = new Gdiplus::Point[srcPoints.GetCount()];

    POSITION position = srcPoints.GetHeadPosition();
    int index = 0;

    while (position)
    {
        Gdiplus::Point & point = srcPoints.GetNext(position);

        points[index].X = point.X;
        points[index].Y = point.Y;

        index++;
    }

    graphics.DrawCurve(&pen, points, srcPoints.GetCount());

    delete[] points;

    Gdiplus::Pen pen2(Color(255, 200, 200, 200), 1);

    graphics.DrawLine(&pen2, Point(0, h2), Point(w, h2));
    graphics.DrawLine(&pen2, Point(w2, 0), Point(w2, h));
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值