11

I'm looking for a nice and efficient implementation of Xiaolin Wu's anti-aliased line drawing algorithm in C, does anyone have this code they could share with me?

Thanks

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
horseyguy
  • 29,455
  • 20
  • 103
  • 145

2 Answers2

13

Wikipedia has pseudo code.

Google has many examples like this one or this one. And your question reminded me this nice article on antialiasing.

EDIT: It's time to discover Hugo Helias's website if you don't know it already.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • thanks the second link was exactly what i wanted. I searched on google myself but for some reason couldn't get these links, thanks! :) – horseyguy Dec 14 '09 at 15:42
  • The Wikipedia pseudocode doesn't work if the line end is to the left or above the line start (IOW it only works increasing X,Y) – smirkingman Dec 09 '19 at 11:57
0

Wondering if implementation here is correct, because in

ErrorAdj = ((unsigned long) DeltaX << 16) / (unsigned long) DeltaY;
      /* Draw all pixels other than the first and last */
while (--DeltaY) {
         ErrorAccTemp = ErrorAcc;   /* remember current accumulated error */
         ErrorAcc += ErrorAdj;      /* calculate error for next pixel */
         if (ErrorAcc <= ErrorAccTemp) {
            /* The error accumulator turned over, so advance the X coord */
            X0 += XDir;
         }
         Y0++; /* Y-major, so always advance Y */
         /* The IntensityBits most significant bits of ErrorAcc give us the
            intensity weighting for this pixel, and the complement of the
            weighting for the paired pixel */
         Weighting = ErrorAcc >> IntensityShift;
         DrawPixel(pDC,X0, Y0, BaseColor + Weighting);
         DrawPixel(pDC,X0 + XDir, Y0,
               BaseColor + (Weighting ^ WeightingComplementMask));
      }

condition if (ErrorAcc <= ErrorAccTemp) is always false.

Sadderdaze
  • 304
  • 3
  • 14