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

  1. /* Listing 6-1 */
  2.  
  3. Line( x1, y1, x2, y2, n )
  4. int    x1,y1;            /* endpoint */
  5. int    x2,y2;            /* endpoint */
  6. int    n;            /* pixel value */
  7. {
  8.     int    x,y;
  9.     float    m;        /* slope */
  10.     float    b;        /* y-intercept */
  11.  
  12.  
  13.     if (x2 == x1)                /* vertical line */
  14.     {
  15.       if (y1 > y2)
  16.        Swap( &y1, &y2 );            /* force y1 < y2 */
  17.  
  18.       for (y=y1; y<=y2; y++)        /* draw from y1 to y2 */
  19.        SetPixel( x1, y, n );
  20.  
  21.       return;
  22.     }
  23.  
  24.     if (x1 > x2)                /* force x1 < x2 */
  25.     {
  26.       Swap( &x1, &x2 );
  27.       Swap( &y1, &y2 );
  28.     }
  29.  
  30.  
  31.     m = (float)(y2-y1) / (float)(x2-x1);    /* compute m and b */
  32.     b = y1 - (m*x1);
  33.  
  34.     for (x=x1; x<=x2; x++)            /* draw from x1 to x2 */
  35.     {
  36.       y = m*x + b;
  37.       SetPixel( x, y, n );
  38.     }
  39. }
  40.  
  41. Swap( a, b )            /* exchange values of a and b */
  42. int    *a,*b;
  43. {
  44.     int    t;
  45.  
  46.     t = *a;
  47.     *a = *b;
  48.     *b = t;
  49. }
  50.