home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_02 / 1n02024a < prev    next >
Text File  |  1990-07-09  |  3KB  |  151 lines

  1. #include    <math.h>
  2. #include    <stdio.h>
  3.  
  4. void    swap( int *pa, int *pb);
  5.  
  6. #define    MAX_X    3179
  7. #define    MAX_Y    1949
  8.  
  9. /******************************************************************
  10. *    lj_line - draw a line on the laser printer's internal page
  11. *
  12. *    Parameters:
  13. *       x1 (in) - starting x coordinate in dots
  14. *        y1 (in) - starting y coordinate in dots
  15. *        x2 (in) - ending x coordinate in dots
  16. *        y2 (in) - ending y coordiante in dots
  17. *
  18. *    Notes:
  19. *        If you may be varying the plotting dimensions (either by
  20. *        changing the output resolution or by changing the page
  21. *        orientation), make MAX_X and MAX_Y into global variables
  22. *        equal to the appropriate dimensions.
  23. *
  24. *    Copyright:
  25. *        Original code by William H. Roetzheim (619) 669-6970
  26. **********************************************************************/
  27.  
  28. void    lj_line(int x1, int y1, int x2, int y2)
  29. {
  30.     int        d, dx, dy;
  31.     int        Aincr, Bincr, xincr, yincr;
  32.     int        x,y;
  33.  
  34.     /* uses modified Bresenham's algorithm */
  35.  
  36.     /* clip if line totally off plotting area */
  37.     if (((x1 < 0) && (x2 < 0)) ||
  38.         ((y1 < 0) && (y2 < 0)) ||
  39.         ((x1 > MAX_X) && (x2 > MAX_X)) ||
  40.         ((y1 > MAX_Y) && (y2 > MAX_Y))) return;
  41.  
  42.     /* test for vertical line */
  43.     if (x1 == x2)
  44.     {
  45.         if (y1 > y2)         /* force y1 < y2 */
  46.  
  47.         {
  48.             swap(&x1, &x2);
  49.             swap(&y1, &y2);
  50.         }
  51.         /* plot vertical line */
  52.         fprintf(stdprn, "\033*p%dx%dY", x1, y1);    /* position to top of line */
  53.         fprintf(stdprn, "\033*c1a%dB", y2 - y1);    /* width = 1, height = line height */
  54.         fprintf(stdprn, "\033*c0P");    /* print the line */
  55.         return;
  56.     }
  57.  
  58.     /* test for horizontal line */
  59.     if (y1 == y2)
  60.     {
  61.         if (x1 > x2)    /* force x1 < x2 */
  62.         {
  63.             swap (&x1, &x2);
  64.             swap (&y1, &y2);
  65.         }
  66.         /* plot horizontal line */
  67.         fprintf(stdprn, "\033*p%dx%dY", x1, y1);    /* position to top of line */
  68.         fprintf(stdprn, "\033*c%da1B", x2 - x1);    /* width = line, height = 1 */
  69.         fprintf(stdprn, "\033*c0P");    /* print the line */
  70.         return;
  71.     }
  72.  
  73.     /* Here's where Bresenham's algorithm really starts */
  74.  
  75.     if (abs(x2 - x1) > abs(y2 - y1))
  76.     {
  77.         if (x1 > x2)    /* force x1 < x2 */
  78.         {
  79.             swap (&x1, &x2);
  80.             swap (&y1, &y2);
  81.         }
  82.         if (y2 > y1) yincr = 1;
  83.         else yincr = -1;
  84.  
  85.         dx = x2 - x1;
  86.         dy = abs(y2 - y1);
  87.         d = 2 * dy - dx;
  88.  
  89.         Aincr = 2 * (dy - dx);
  90.         Bincr = 2 * dy;
  91.  
  92.         x = x1;
  93.         y = y1;
  94.  
  95.         set_pixel(x, y);
  96.  
  97.         for (x = x1 + 1; x <= x2; x++)
  98.         {
  99.             if (d >= 0)
  100.             {
  101.                 y += yincr;
  102.                 d += Aincr;
  103.             }
  104.             else d += Bincr;
  105.             set_pixel(x, y);
  106.         }
  107.     }
  108.     else
  109.     {
  110.         if (y1 > y2)    /* force y1 < y2 */
  111.         {
  112.             swap (&x1, &x2);
  113.             swap (&y1, &y2);
  114.         }
  115.  
  116.         if (x2 > x1) xincr = 1;
  117.         else xincr = -1;
  118.  
  119.         dx = abs(x2 - x1);
  120.         dy = y2 - y1;
  121.         d = 2 * dx - dy;
  122.  
  123.         Aincr = 2 * (dx - dy);
  124.         Bincr = 2 * dx;
  125.  
  126.         x = x1;
  127.         y = y1;
  128.  
  129.         set_pixel(x, y);
  130.  
  131.         for (y = y1 + 1; y <= y2; y++)
  132.         {
  133.             if (d >= 0)
  134.             {
  135.                 x += xincr;
  136.                 d += Aincr;
  137.             }
  138.             else d += Bincr;
  139.             set_pixel(x, y);
  140.         }
  141.     }
  142. }
  143.  
  144. void    swap( int *pa, int *pb)
  145. {
  146.     int        t;
  147.  
  148.     t = * pa;
  149.     *pa = *pb;
  150.     *pb = t;
  151. }