Hi! Can someone please give me some advice on the following program? I'm
trying to draw some antialiased lines, and if you'll run the program,
you'll see that they all work, EXCEPT for the one that runs from the upper
left to the lower right. Can anyone shed any light on what I'm doing
wrong?

Thanks!

-J

========================================================================

#include <stdlib.h>
#include <vga.h>
#include <vgagl.h>

void wu_line(int x1, int y1, int x2, int y2, int r, int g, int b);

int main(void)
{
   vga_init();
   vga_setmode(G320x200x256);
   gl_setcontextvga(G320x200x256);
   gl_setrgbpalette();

   wu_line(300, 10, 70, 180, 255, 255, 255);
   wu_line(0, 10, 300, 185, 255, 255, 255);
   wu_line(100, 10, 70, 180, 255, 255, 255);
   wu_line(100, 10, 130, 180, 255, 255, 255);
   wu_line(0, 100, 300, 120, 255, 255, 255);
   vga_getch();

   vga_setmode(TEXT);

   return 0;
}

void wu_line(int x1, int y1, int x2, int y2, int r, int g, int b)
{
    #define ABS(a) (((a)<0) ? -(a) : (a))

    int temp;
    int dx, dy, ax, ay, sx, sy, x, y;
    int ox, oy, nx, ny;
    int r1, g1, b1, r2, g2, b2;
    int highr, lowr, highg, lowg, highb, lowb;
    int percent;

    if (x1 > x2)
    {
       temp = x1;
       x1 = x2;
       x2 = temp;
       temp = y1;
       y1 = y2;
       y2 = temp;
    }

    dx = x2 - x1;
    dy = y2 - y1;
    ax = ABS(dx) << 1;
    ay = ABS(dy) << 1;
    sx = (dx >= 0) ? 1 : -1;
    sy = (dy >= 0) ? 1 : -1;

    x = x1;
    y = y1;

    if (ax > ay)
    {
        int d = ay - (ax >> 1);
        oy = y;
        while (x != x2)
        {
            gl_setpixelrgb(x, y, r, g, b);

            if (d > 0 || (d == 0 && sx == 1))
            {
                y += sy;
                oy = y - 1;
                d -= ax;
            }
            x += sx;
            d += ay;
            ny = y + 1;
            percent = ((ABS(d) * 100) / ax);

            gl_getpixelrgb(x, oy, &r1, &g1, &b1);
            highr = (r > r1) ? r : r1;
            lowr = (r == highr) ? r1 : r;
            r2 = (((highr - lowr) * percent) / 100) + lowr;
            highg = (g > g1) ? g : g1;
            lowg = (g == highg) ? g1 : g;
            g2 = (((highg - lowg) * percent) / 100) + lowg;
            highb = (b > b1) ? b : b1;
            lowb = (b == highb) ? b1 : b;
            b2 = (((highb - lowb) * percent) / 100) + lowb;
            gl_setpixelrgb(x, oy, r2, g2, b2);            

            gl_getpixelrgb(x, ny, &r1, &g1, &b1);
            highr = (r > r1) ? r : r1;
            lowr = (r == highr) ? r1 : r;
            r2 = 255 - ((((highr - lowr) * percent) / 100) + lowr);
            highg = (g > g1) ? g : g1;
            lowg = (g == highg) ? g1 : g;
            g2 = 255 - ((((highg - lowg) * percent) / 100) + lowg);
            highb = (b > b1) ? b : b1;
            lowb = (b == highb) ? b1 : b;
            b2 = 255 - ((((highb - lowb) * percent) / 100) + lowb);
            gl_setpixelrgb(x, ny, r2, g2, b2);
        }
    } else {
        int d = ax - (ay >> 1);
        ox = x;
        while (y != y2)
        {
            gl_setpixelrgb(x, y, r, g, b);

            if (d > 0 || (d == 0 && sy == 1))
            {
                x += sx;
                ox = x - 1;
                d -= ay;
            }
            y += sy;
            d += ax;
            nx = x + 1;
            percent = ((ABS(d) * 100) / ay);

            gl_getpixelrgb(ox, y, &r1, &g1, &b1);
            highr = (r > r1) ? r : r1;
            lowr = (r == highr) ? r1 : r;
            r2 = (((highr - lowr) * percent) / 100) + lowr;
            highg = (g > g1) ? g : g1;
            lowg = (g == highg) ? g1 : g;
            g2 = (((highg - lowg) * percent) / 100) + lowg;
            highb = (b > b1) ? b : b1;
            lowb = (b == highb) ? b1 : b;
            b2 = (((highb - lowb) * percent) / 100) + lowb;
            gl_setpixelrgb(ox, y, r2, g2, b2);

            gl_getpixelrgb(nx, y, &r1, &g1, &b1);
            highr = (r > r1) ? r : r1;
            lowr = (r == highr) ? r1 : r;
            r2 = 255 - ((((highr - lowr) * percent) / 100) + lowr);
            highg = (g > g1) ? g : g1;
            lowg = (g == highg) ? g1 : g;
            g2 = 255 - ((((highg - lowg) * percent) / 100) + lowg);
            highb = (b > b1) ? b : b1;
            lowb = (b == highb) ? b1 : b;
            b2 = 255 - ((((highb - lowb) * percent) / 100) + lowb);
            gl_setpixelrgb(nx, y, r2, g2, b2);
        }
    }
    gl_setpixelrgb(x, y, r, g, b);

    return;
}

Reply via email to