兄弟对图形处理方面没有什么研究。手头有一项目比较急,其中需要画一些直线,用LineTo试了一下,直线有锯齿,不太美观。求各位大哥给一个反走样算法,有源码更好,谢谢了!

解决方案 »

  1.   

    http://articles.gameres.com/Program/Visual/Effects/WuLine.htm
      

  2.   

    在codeguru 上有Sample code,可能没有引起足够重视,只有一个贴子
    http://www.codeguru.com/gdi/GDIPlus.html
    当然,msdn上资料更多,在http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/cpp_gdi+start_26ic.asp
    慢慢看吧,不过我觉得仍感不够过,所以最好的办法是边看边实践
    如果你觉得有用,给我加分吧
      

  3.   

    找到一段WuLine的代码,delphi的,移植过来了:
    // anti-aliased line
    void CMapView::WuLine(CDC* pDC, CPoint& pt1, CPoint& pt2, int color)
    {
        int deltax, deltay, start, finish;
        double dx, dy, dydx; // fractional parts
        BYTE LR, LG, LB;
    int x1, x2, y1, y2;
    double d1, d2;
            
        x1 = pt1.x; y1 = pt1.y;
        x2 = pt2.x; y2 = pt2.y;
        
        deltax = abs(x2 - x1); // Calculate deltax and deltay for initialisation
        deltay = abs(y2 - y1);
        
        if((deltax == 0)||(deltay == 0))
        {
         pDC->MoveTo(x1, y1);
         pDC->LineTo(x2, y2);
         return;
        }
        
        LR = color & 0x0000FF;
        LG = (color & 0x00FF00) >> 8;
        LB = (color & 0xFF0000) >> 16;
        
        if(deltax > deltay) // then begin // horizontal or vertical
        {
        if(y2 > y1) // determine rise and run
         dydx = -((double)deltay / deltax);
        else
         dydx = (double)deltay / deltax;
        if(x2 < x1)
        {
        start = x2;  // right to left
        finish = x1;
        dy = y2;
    }
        else
        {
         start = x1;  // left to right
         finish = x2;
         dy = y1;
         dydx = -dydx;  // inverse slope
        }
        
        for (int x = start; x <= finish; x++)
        {
    d2 = modf(dy, &d1);     AlphaBlendPixel(pDC, x, (int)d1, LR, LG, LB, 1 - d2);
        AlphaBlendPixel(pDC, x, (int)d1 + 1, LR, LG, LB, d2);
        dy = dy + dydx; // next point
    }
    }
        else
        {
         if(x2 > x1) // determine rise and run
         dydx = -((double)deltax / deltay);
        else
         dydx = (double)deltax / deltay;
        
         if(y2 < y1)
         {
        start = y2;  // right to left
        finish = y1;
        dx = x2;
    }
    else
    {
        start = y1;  // left to right
        finish = y2;
        dx = x1;
        dydx = -dydx;  // inverse slope
    } for(int y = start; y <= finish; y++)
    {
    d2 = modf(dx, &d1);     AlphaBlendPixel(pDC, (int)d1, y, LR, LG, LB, 1 - d2);
        AlphaBlendPixel(pDC, (int)d1 + 1, y, LR, LG, LB, d2);
        dx = dx + dydx;
    }
    }
    }// blend a pixel with the current colour and a specified colour
    void CMapView::AlphaBlendPixel(CDC* pDC, int x, int y, BYTE R, BYTE G, BYTE B, double ratio)
    {
        double minus_ratio;
        int color_old;
        BYTE R1, G1, B1;
        BYTE R2, G2, B2;
        
        if((x < 0) || (x >= m_iWidth) || (y < 0) || (y >= m_iHeight))
         return;
        
        color_old = pDC->GetPixel(x, y);    
        R1 = color_old & 0x0000FF;
        G1 = (color_old & 0x00FF00) >> 8;
        B1 = (color_old & 0xFF0000) >> 16;
     
        minus_ratio = 1 - ratio;
        B2 = (int)(B*ratio + B1*minus_ratio);
        G2 = (int)(G*ratio + G1*minus_ratio);
        R2 = (int)(R*ratio + R1*minus_ratio);
            
        pDC->SetPixel(x, y, RGB(R2, G2, B2));
    }