home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / video / 6 / 6_2.c < prev    next >
Encoding:
Text File  |  1988-08-11  |  948 b   |  60 lines

  1. /* Listing 6-2 */
  2.  
  3. Line( x1, y1, x2, y2, n )    /* for lines with slope between -1 and 1 */
  4. int    x1,y1;
  5. int    x2,y2;            /* endpoints */
  6. int    n;            /* pixel value */
  7. {
  8.     int    d,dx,dy;
  9.     int    Aincr,Bincr,yincr;
  10.     int    x,y;
  11.  
  12.  
  13.     if (x1 > x2)                /* force x1 < x2 */
  14.     {
  15.       Swap( &x1, &x2 );
  16.       Swap( &y1, &y2 );
  17.     }
  18.  
  19.     if (y2 > y1)                /* determine increment for y */
  20.       yincr = 1;
  21.     else
  22.       yincr = -1;
  23.  
  24.     dx = x2 - x1;                /* initialize constants */
  25.     dy = abs( y2-y1 );
  26.     d = 2 * dy - dx;
  27.  
  28.     Aincr = 2 * (dy - dx);
  29.     Bincr = 2 * dy;
  30.  
  31.     x = x1;                    /* initial x and y */
  32.     y = y1;
  33.  
  34.     SetPixel( x, y, n );            /* set pixel at (x1,y1) */
  35.  
  36.     for (x=x1+1; x<=x2; x++)        /* do from x1+1 to x2 */
  37.     {
  38.       if (d >= 0)
  39.       {
  40.         y += yincr;                /* set pixel A */
  41.         d += Aincr;
  42.       }
  43.       else                    /* set pixel B */
  44.         d += Bincr;
  45.  
  46.       SetPixel( x, y, n );
  47.     }
  48. }
  49.  
  50.  
  51. Swap( pa, pb )
  52. int    *pa,*pb;
  53. {
  54.     int    t;
  55.  
  56.     t = *pa;
  57.     *pa = *pb;
  58.     *pb = t;
  59. }
  60.