/////////////////////画曲线函数////////////////////////////
////////////////////直角坐标下的////////////////////////
//y=fun(x)
//p---绘图设备指针
//fun---函数指针
//StartP1---x的起始值
//StartP2---x的终止值
//PX---坐标原点x方向位置
//PY---坐标原点y方向位置
//Mx---x方向放大倍数
//My---y方向放大倍数
/////////////////////////////////////////////////////////
void DrawCurve(HDC hDC,double (*fun)(double),double StartP1,double EndP1,int PX,int PY,double Mx,double My)
{
int i,N=int(Mx*(EndP1-StartP1));
double x,y,dx;
dx=(EndP1-StartP1)/N;
x=StartP1;
y=(*fun)(x);
MoveToEx(hDC,PX+int(x*Mx),PY-int(y*My),NULL);
for(i=1;i<=N;i++)
{
x=StartP1+i*dx;
y=(*fun)(x);
LineTo(hDC,PX+int(x*Mx),PY-int(y*My));
}

}

解决方案 »

  1.   

    最好是基于我那个坐标轴,我弄了个代码是///以下绘制扭矩曲线
     //定义曲线颜色,红色
            CPen newpen1;
            CPen *poldpen1;
            newpen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
        poldpen1=m_dcMem.SelectObject(&newpen1);     m_dcMem.MoveTo(rectStaticClient.left-60,rectStaticClient.bottom-240);        for(int b=0;b<25200;b++)   
    {              double   x=b+rectStaticClient.left-60; 
           double   y=sin(x/20); 
               CPoint   pt(x,y*30+rectStaticClient.bottom-240);   
     
               if(b==0)  
       m_dcMem.MoveTo(pt);   
               else                  
       m_dcMem.LineTo(pt);   }  显示不出,不知道哪里错了
      

  2.   

    有谁能帮帮我吗,把SIN去掉就可以显示直线,正玄曲线就显示不了,不知道为什么
      

  3.   

    我的代码完全可以显示啊
    调用下面代码,正弦曲线就出来了
    DrawCurve(m_dcMem.m_hDC,sin,0,6.28,300,300,30,30);
      

  4.   

    void CFangdaDlg::drawGra()
    {
    CPaintDC dc(this);       CPen newpen;
         CPen* poldpen;
    newpen.CreatePen(PS_SOLID,1,RGB(179,255,255));
         poldpen=m_dcMem.SelectObject(&newpen);//绘制坐标轴 int M=40;
    //横轴
         m_dcMem.MoveTo(rectStaticClient.left-60,rectStaticClient.bottom-100-M);
    m_dcMem.LineTo(25200,rectStaticClient.bottom-100-M); m_dcMem.LineTo(25200-40,rectStaticClient.bottom-103-M);
    m_dcMem.LineTo(25200,rectStaticClient.bottom-100-M);
        m_dcMem.LineTo(25200-40,rectStaticClient.bottom-97-M);
    //横轴刻度 int k=100000;
    for(int x=0;x<k+1;x++)
    {          
               if(x%25==0)
       {
          m_dcMem.MoveTo((int)(rectStaticClient.left+x-60),
            (int)(rectStaticClient.bottom-100-M));
              m_dcMem.LineTo((int)(rectStaticClient.left+x-60),
            (int)(rectStaticClient.bottom-105-M));    }
       if(x%125==0)
       {
          m_dcMem.MoveTo((int)(rectStaticClient.left+x-60),
            (int)(rectStaticClient.bottom-100-M));
              m_dcMem.LineTo((int)(rectStaticClient.left+x-60),
            (int)(rectStaticClient.bottom-110-M));              XPos.Format("%d",4*x);
      Xsize=dc.GetTextExtent(XPos,XPos.GetLength());   m_dcMem.SetTextColor(RGB (0, 255, 255));
      m_dcMem.SetBkColor(RGB (0,0,0));       m_dcMem.TextOut((int)(rectStaticClient.left+x-60-Xsize.cx/2),
             rectStaticClient.bottom-85-M,XPos);
       }
    }        
            m_dcMem.MoveTo(rectStaticClient.left-60,rectStaticClient.bottom-100-M);
            m_dcMem.LineTo(rectStaticClient.left-60,rectStaticClient.bottom-400-M);
            m_dcMem.LineTo(rectStaticClient.left-62,rectStaticClient.bottom-390-M);
            m_dcMem.LineTo(rectStaticClient.left-60,rectStaticClient.bottom-400-M);
            m_dcMem.LineTo(rectStaticClient.left-58,rectStaticClient.bottom-390-M);
            int l=1000;
    for(int y=0;y<l+1;y++)
    {          
               if(y%100==0)
       {
          m_dcMem.MoveTo((int)(rectStaticClient.left-60),
            (int)(rectStaticClient.bottom-100-M-280*y/1000));
              m_dcMem.LineTo((int)(rectStaticClient.left-54),
            (int)(rectStaticClient.bottom-100-M-280*y/1000));
       }
    }           for(y=0;y<l+1;y++)
       {
                  if(y%20==0)
      {
            m_dcMem.MoveTo((int)(rectStaticClient.left-60),
            (int)(rectStaticClient.bottom-100-M-280*y/1000));
                m_dcMem.LineTo((int)(rectStaticClient.left-57),
            (int)(rectStaticClient.bottom-100-M-280*y/1000));
      }
       }////以下绘制扭矩曲线
     //定义曲线颜色,红色
            CPen newpen1;
            CPen *poldpen1;
            newpen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
        poldpen1=m_dcMem.SelectObject(&newpen1);     m_dcMem.MoveTo(rectStaticClient.left-60,rectStaticClient.bottom-240);        for(int b=0;b<25200;b++)   
    {              double   x=b+rectStaticClient.left-60; 
           double   y=sin(x/20); 
               CPoint   pt(x,y*30+rectStaticClient.bottom-240);   
     
               if(b==0)  
       m_dcMem.MoveTo(pt);   
               else                  
       m_dcMem.LineTo(pt);   } 
      m_dcMem.SelectObject(poldpen1);///以下为通过函数BitBlt()来显示上面画的全部内容:黑色背景,坐标,刻度,曲线 dc.BitBlt(offsetx,offsety+10,m_size.cx,m_size.cy-20, 
                      &m_dcMem, sourcex, sourcey,SRCCOPY);}全部在这了,不知道哪出了问题,谢谢了