home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / digitall.c < prev    next >
C/C++ Source or Header  |  1992-04-09  |  957b  |  55 lines

  1. /* 
  2. Digital Line Drawing
  3. by Paul Heckbert
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. /*
  8.  * digline: draw digital line from (x1,y1) to (x2,y2),
  9.  * calling a user-supplied procedure at each pixel.
  10.  * Does no clipping.  Uses Bresenham's algorithm.
  11.  * Paul Heckbert    3 Sep 85
  12.  */
  13.  
  14. #include "GraphicsGems.h"
  15. digline(x1, y1, x2, y2, dotproc)
  16. int x1, y1, x2, y2;
  17. void (*dotproc)();
  18. {
  19.     int d, x, y, ax, ay, sx, sy, dx, dy;
  20.  
  21.     dx = x2-x1;  ax = ABS(dx)<<1;  sx = SGN(dx);
  22.     dy = y2-y1;  ay = ABS(dy)<<1;  sy = SGN(dy);
  23.  
  24.     x = x1;
  25.     y = y1;
  26.     if (ax>ay) {        /* x dominant */
  27.         d = ay-(ax>>1);
  28.         for (;;) {
  29.             (*dotproc)(x, y);
  30.             if (x==x2) return;
  31.             if (d>=0) {
  32.                 y += sy;
  33.                 d -= ax;
  34.             }
  35.             x += sx;
  36.             d += ay;
  37.         }
  38.     }
  39.     else {            /* y dominant */
  40.         d = ax-(ay>>1);
  41.         for (;;) {
  42.             (*dotproc)(x, y);
  43.             if (y==y2) return;
  44.             if (d>=0) {
  45.                 x += sx;
  46.                 d -= ay;
  47.             }
  48.             y += sy;
  49.             d += ax;
  50.         }
  51.    }
  52. }
  53.  
  54.  
  55.