c++ - 画线算法

标签 c++ c graphics

我使用 wu 技术编写了一个画线算法。它有效但有问题。见下文。

我想知道是否有人解决了绘制抗锯齿线的问题?令人惊讶的是,谷歌搜索结果没有令人满意的算法。有很多讨论,但没有一个完整的、健壮的算法,独立于对图形库的任何依赖。

如果有人想要以下函数的示例代码,请告诉我。我希望用更强大的功能替换它,但我还没有找到。我确信我的算法可以改进,但它确实有效。

struct xya
{
    int x;
    int y; 
    uint8_t a;
    xya(int x=0, int y=0, uint8_t a=0) : x(x), y(y), a(a) {}
};

inline void wuline(xya*& out, int x0, int y0, int x1, int y1)
{
    short DeltaX, DeltaY, XDir;
    static const int intensity = 8;

    if (y0 > y1)
    {
        short Temp = y0; 
        y0 = y1; 
        y1 = Temp;
        Temp = x0; 
        x0 = x1; 
        x1 = Temp;
    }

    *out++ = xya(x0,y0,255);

    if ((DeltaX = x1 - x0) >= 0)
    {
        XDir = 1;
    } 
    else 
    {
        XDir = -1;
        DeltaX = -DeltaX; 
    }

    if ((DeltaY = y1 - y0) == 0)
    {
        while (DeltaX-- != 0) 
        {
            x0 += XDir;
            *out++ = xya(x0,y0,255);            
        }

        return;
    }

    if (DeltaX == 0) 
    {
        do 
        {
            y0++;
            *out++ = xya(x0,y0,255);            
        } 
        while (--DeltaY != 0);

        return;
    }

    if (DeltaX == DeltaY) 
    {
        do 
        {
            x0 += XDir;
            y0++;
            *out++ = xya(x0,y0,255);            
        } 
        while (--DeltaY != 0);

        return;
    }

    if (DeltaY > DeltaX) 
    {
        unsigned short ErrorAcc = 0;  
        unsigned short ErrorAdj = ((unsigned long) DeltaX > intensity;
            *out++ = xya(x0,y0,Weighting ^ 255);
            *out++ = xya(x0+XDir,y0,Weighting);
        }

        *out++ = xya(x1,y1,255);            
    }
    else
    {
        unsigned short ErrorAcc = 0;  
        unsigned short ErrorAdj = ((unsigned long) DeltaY > intensity;
            *out++ = xya(x0,y0,Weighting ^ 255);            
            *out++ = xya(x0,y0+1,Weighting);
        }

        *out++ = xya(x1,y1,255);
    }
}

最佳答案

快速有效地绘制抗锯齿线的典型示例是 Xiaolin Wu's algorithm 。您可能需要查看它以获得可靠的方法。这是 some sample code , 也。右图应用吴氏算法的结果:

alt text http://www.suchit-tiwari.org/writings/antialias/antialias.png

关于c++ - 画线算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2025451/

相关文章:

c++ - 字符串分配期间访问冲突写入位置错误

c++ - fork 、信号以及它们如何与 C 中的全局变量交互

c++ - VS 2015 图形调试器给我源错误

C - fprintf 未写入文件

java - Java中删除对象

java - 在java中表示二维球堆

c++ - 在 Ctrl+Z (EOF) 后恢复从 iostream::cin 读取? ("ignore"不起作用)

c++ - dynamic_cast 没有按预期抛出异常

C:多个 fork

C程序对数字进行四舍五入,四舍五入到小数点后第二位