Linear drawing algorithm

I wrote a line drawing algorithm using the wu technique. It works, but has problems. See below.

I am wondering if anyone can solve the problem of drawing smooth lines? Surprisingly, there are no satisfactory algorithms in the Google search results. A lot of discussions, but not one complete, reliable algorithm, regardless of any dependencies on the graphics library.

If someone wants an example code for the next function to let me know. I hope to replace this with a more robust feature, but I haven't found it yet. I'm sure my algorithm can be improved, but it works.

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);
    }
}

+3
source share
3 answers

A canonical example of fast and efficient drawing of smooth lines is Xiaolin Wu algorithm . You might want to take a look at this for a reliable approach. Here is a sample code . The result of applying the Wu algorithm is on the right:

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

+3

, ? , Windows GDI + , , , , QT WX. , OpenGL .

GDI + http://msdn.microsoft.com/en-us/library/ms535723%28VS.85%29.aspx

, .

+1

- , . , (2x? 3x?), , . R, G B , .

-, .

0

Source: https://habr.com/ru/post/1727725/


All Articles