關於畫圖底層演算法

來源:互聯網
上載者:User

平時, 用慣了API、CDC和封裝好的內建函式,關於畫線,畫圓這些東西都是信手拈來…

    最近學習電腦圖形學,不得不深入內部研究一下底層演算法…

    在這裡貼幾個代碼分享一下!

    (這裡只給出MFC的OnDraw函數)

  一、畫線的三個演算法:

    1。DDA(數值微分)法:

    void CDDALineView::OnDraw(CDC* pDC)

    {

    CDDALineDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here

    int x,x0(200),y0(200),x1(500),y1(500);

    float dx,dy,y,k;

    dx=x1-x0;

    dy=y1-y0;

    k=dy/dx;

    y=y0;

    for(x=x0;x<=x1;x++)

    {

    pDC->SetPixel(x,(int)(y+0.5),RGB(255,0,0));

    y+=k;

    }

    }

    2.中點畫線演算法

    void CMidpointLineView::OnDraw(CDC* pDC)

    {

    CMidpointLineDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here

    int a,b,d1,d2,d,x,y;

    int x0(200),x1(500),y0(200),y1(500);

    a=y0-y1;

    b=x1-x0;

    d=2*a+b;

    d1=2*a;

    d2=2*(a+b);

    x=x0;

    y=y0;

    pDC->SetPixel(x,y,RGB(0,255,0));

    while(x<x1)

    {

    if(d<0)

    {

    x++;

    y++;

    d+=d2;

    }

    else

    {

    x++;

    d+=d1;

    }

    pDC->SetPixel(x,y,RGB(0,255,0));

    }

    }

    3。Bresenham演算法:

    void CBresenhamline2View::OnDraw(CDC* pDC)

    {

    CBresenhamline2Doc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here

    int x0(200),y0(200),x1(500),y1(500);

    int x,y,dx,dy;

    dx=x1-x0;

    dy=y1-y0;

    int e=-dx;

    x=x0;

    y=y0;

    for(int i=0;i<=dx;i++)

    {

    pDC->SetPixel(x,y,RGB(0,0,255));

    x=x+1;

    e=e+2*dy;

    if(e>=0)

    {

    y++;

    e=e-2*dx;

    }

    }

    }

  二、中點畫圓演算法:

      void CMidPointCircleView::OnDraw(CDC* pDC)

    {

    CMidPointCircleDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here

    int r=100;            //半徑

    int m(300),n(250);    //圓心座標

    int x,y;

    float d;

    x=0;

    y=0+r;

    d=1.25-r;

    //中點畫圓演算法

    pDC->SetPixel(m+x,n+y,RGB(255,0,0));

    pDC->SetPixel(m+y,n+x,RGB(255,0,0));

    pDC->SetPixel(m-x,n+y,RGB(255,0,0));

    pDC->SetPixel(m+y,n-x,RGB(255,0,0));

    pDC->SetPixel(m+x,n-y,RGB(255,0,0));

    pDC->SetPixel(m-y,n+x,RGB(255,0,0));

    pDC->SetPixel(m-x,n-y,RGB(255,0,0));

    pDC->SetPixel(m-y,n-x,RGB(255,0,0));

    while(x<=y)

    {

    if(d<0)

    d+=2*x+3;

    else

    {

    d+=2*(x-y)+5;

    y--;

    }

    x++;

    pDC->SetPixel(m+x,n+y,RGB(255,0,0));

    pDC->SetPixel(m+y,n+x,RGB(255,0,0));

    pDC->SetPixel(m-x,n+y,RGB(255,0,0));

    pDC->SetPixel(m+y,n-x,RGB(255,0,0));

    pDC->SetPixel(m+x,n-y,RGB(255,0,0));

    pDC->SetPixel(m-y,n+x,RGB(255,0,0));

    pDC->SetPixel(m-x,n-y,RGB(255,0,0));

    pDC->SetPixel(m-y,n-x,RGB(255,0,0));

    }

    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.