home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / GRLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  2.2 KB  |  92 lines

  1. /**
  2. *
  3. * Name        grline -- Draw a colored line
  4. *
  5. * Synopsis    npts = grline(pstart,pend,color);
  6. *
  7. *        int npts      The number of points plotted is returned
  8. *        PT *pstart      Pointer to starting point of line
  9. *        PT *pend      Pointer to ending point of line
  10. *        int color      Color of the line; chosen from the
  11. *                  currently set palette.
  12. *
  13. * Description    This function draws a line on the current display page
  14. *        from *pstart to *pend using the specified color.  The
  15. *        actual number of points plotted to draw the line is
  16. *        returned.  If *pstart and *pend represent the same
  17. *        location, only one point is plotted.
  18. *
  19. * Method    The variables x and y keep count of when the tracing
  20. *        point coordinates whould be incremented.  The variable
  21. *        fplot is used as a flag when a new point is plotted.
  22. *        The same point is not plotted twice, because a color
  23. *        value greater than 128 should XOR the current point.
  24. *
  25. * Returns    npts          Number of points plotted to trace the line.
  26. *
  27. * Version    3.0 (C)Copyright Blaise Computing Inc.    1983, 1984, 1986
  28. *
  29. **/
  30.  
  31. #include <bgraph.h>
  32. #include <butility.h>
  33.  
  34. #define TRUE     1
  35. #define FALSE     0
  36. #define abs(x)     ((x)<0?(-(x)):(x))
  37. #define sign(a)  ((a)>0?1:((a)==0?0:(-1)))
  38.  
  39. int grline(pstart,pend,color)
  40. PT  *pstart,*pend;
  41. int color;
  42. {
  43.     int deltax,deltay,steps,i;
  44.     int incx,incy,x,y,fplot,npts;
  45.     PT    pos;
  46.  
  47.     /* First set up the increments and determine how many points must  */
  48.     /* be plotted to trace the line.                       */
  49.  
  50.     deltax = pend->x - pstart->x;
  51.     deltay = pend->y - pstart->y;
  52.     incx   = abs(deltax);
  53.     incy   = abs(deltay);
  54.     steps  = max(incx,incy);
  55.  
  56.     /* Initialize the counting variable, plot the first point and     */
  57.     /* trace the rest of the line.                      */
  58.  
  59.     x      = 0;
  60.     y      = 0;
  61.     pos.x = pstart->x;
  62.     pos.y = pstart->y;
  63.  
  64.     npts = 1;
  65.     grptwrit(&pos,color);
  66.     for (i = 0; i <= steps; i++)
  67.     {
  68.     fplot = FALSE;
  69.     x += incx;
  70.     y += incy;
  71.     if (y > steps)
  72.     {
  73.        y     -= steps;
  74.        pos.y += sign(deltay);
  75.        fplot  = TRUE;
  76.     }
  77.     if (x > steps)
  78.     {
  79.        x     -= steps;
  80.        pos.x += sign(deltax);
  81.        fplot  = TRUE;
  82.     }
  83.     if (fplot)              /* Only plot if a new point      */
  84.     {
  85.        grptwrit(&pos,color);
  86.        npts++;
  87.     }
  88.     }
  89.  
  90.     return(npts);
  91. }
  92.