Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Does anyone have a C code to graph some antialiased lines?

Status
Not open for further replies.

Anaxetus

Newbie level 5
Joined
Jul 5, 2005
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,370
Antialiased lines

Does anyone has a good C code to graph some "antialiased" lines. That is lines with some dither arround (like in an analog scope).
It is intended to be used in PC programming enviroment.

Regards,
 

mik27

Newbie level 5
Joined
Jan 31, 2005
Messages
10
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,281
Activity points
158
Re: Antialiased lines

Try this ...

I assume that:

The color (r, g, b) are float in [0, 1] space (you can easy change it in to [0,255])
VideoBuffer is a 'unsigned char *' (unsigned char pointer) to RGB 8/8/8 = 24bit video buffer
VideoDimX, VideoDimY is the resolution X and Y of VideoBuffer


Code:
void WritePixel(const int x,const int y, 
const float r, const float g, const float b, const float alpha)
{

    //3 is for RGB 24bit deepth
    unsigned char* pVideo=VideoBuffer+(x+(VideoDimX-(y+1))*VideoDimY)*3; 
    pVideo[0]=(alpha*r)*255+(1.0f-alpha)*pVideo[0];
    pVideo[1]=(alpha*g)*255+(1.0f-alpha)*pVideo[1];
    pVideo[2]=(alpha*b)*255+(1.0f-alpha)*pVideo[2];
}

void LineAA(const int x1,const int y1,const int x2,const int const y2,
                     const float r,const float  g, const float b)
{
    // useful constants
    const float dw=x2-x1;
    const float dh=y2-y1;
    const float slx=dh/dw;
    const float sly=dw/dh;

    // determine the slope
    if(fabs(slx)<1.0)
    {
        // x scan
        int tx1=x1;
        int tx2=x2;
        float raster=y1;

        if(x1>x2)
        {
            tx1=x2;
            tx2=x1;
            raster=y2;
        }

        for(int x=tx1;x<=tx2;x++)
        {
            const int ri=int(raster);

            const float AlphaY0=1.0-(raster-ri);
            const float AlphaY1=1.0-(ri+1-raster);

            WritePixel(x,ri  ,r,g,b,AlphaY0);
            WritePixel(x,ri+1,r,g,b,AlphaY1);

            raster+=slx;
        }
    }
    else
    {
        // y scan
        int ty1=y1;
        int ty2=y2;
        float raster=x1;

        if(y1>y2)
        {
            ty1=y2;
            ty2=y1;
            raster=x2;
        }

        for(int y=ty1;y<=ty2;y++)
        {
            const int ri=int(raster);

            const float AlphaX0=1.0-(raster-ri);
            const float AlphaX1=1.0-(ri+1-raster);

            WritePixel(ri  ,y,r,g,b,AlphaX0);
            WritePixel(ri+1,y,r,g,b,AlphaX1);

            raster+=sly;
        }
    }
}

I have made some modifications in order to generalize the algorithm and to render it more readable (is not optimized!) ... but, after, I have not tried it ... tell me if you have any problem

regards ... M!k27
 

    Anaxetus

    Points: 2
    Helpful Answer Positive Rating

mik27

Newbie level 5
Joined
Jan 31, 2005
Messages
10
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,281
Activity points
158
Re: Antialiased lines

Another thing ... I remember that I had written it for a reverse Y VideoBuffer (like Windows DIB ... left -> right, DOWN -> UP) ... for a 'standard' frame VideoBuffer with the origin in left top corner (left -> right, up -> down) you must change the line in the previous code ...

Code:
unsigned char* pVideo=VideoBuffer+(x+(VideoDimX-(y+1))*VideoDimY)*3;

in the line ...
Code:
unsigned char* pVideo=VideoBuffer+(y*VideoDimX+x)*3;



bye ... m!k27
 

    Anaxetus

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top