home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name grline -- Draw a colored line
- *
- * Synopsis npts = grline(pstart,pend,color);
- *
- * int npts The number of points plotted is returned
- * PT *pstart Pointer to starting point of line
- * PT *pend Pointer to ending point of line
- * int color Color of the line; chosen from the
- * currently set palette.
- *
- * Description This function draws a line on the current display page
- * from *pstart to *pend using the specified color. The
- * actual number of points plotted to draw the line is
- * returned. If *pstart and *pend represent the same
- * location, only one point is plotted.
- *
- * Method The variables x and y keep count of when the tracing
- * point coordinates whould be incremented. The variable
- * fplot is used as a flag when a new point is plotted.
- * The same point is not plotted twice, because a color
- * value greater than 128 should XOR the current point.
- *
- * Returns npts Number of points plotted to trace the line.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1983, 1984, 1986
- *
- **/
-
- #include <bgraph.h>
- #include <butility.h>
-
- #define TRUE 1
- #define FALSE 0
- #define abs(x) ((x)<0?(-(x)):(x))
- #define sign(a) ((a)>0?1:((a)==0?0:(-1)))
-
- int grline(pstart,pend,color)
- PT *pstart,*pend;
- int color;
- {
- int deltax,deltay,steps,i;
- int incx,incy,x,y,fplot,npts;
- PT pos;
-
- /* First set up the increments and determine how many points must */
- /* be plotted to trace the line. */
-
- deltax = pend->x - pstart->x;
- deltay = pend->y - pstart->y;
- incx = abs(deltax);
- incy = abs(deltay);
- steps = max(incx,incy);
-
- /* Initialize the counting variable, plot the first point and */
- /* trace the rest of the line. */
-
- x = 0;
- y = 0;
- pos.x = pstart->x;
- pos.y = pstart->y;
-
- npts = 1;
- grptwrit(&pos,color);
- for (i = 0; i <= steps; i++)
- {
- fplot = FALSE;
- x += incx;
- y += incy;
- if (y > steps)
- {
- y -= steps;
- pos.y += sign(deltay);
- fplot = TRUE;
- }
- if (x > steps)
- {
- x -= steps;
- pos.x += sign(deltax);
- fplot = TRUE;
- }
- if (fplot) /* Only plot if a new point */
- {
- grptwrit(&pos,color);
- npts++;
- }
- }
-
- return(npts);
- }