1 /* $Header: /home/yav/xpx/RCS/line.c,v 1.4 1995/11/23 16:28:39 yav Exp $
2  * line
3  * written by yav (UHD98984@pcvan.or.jp)
4  * Reference:
5  * Algorithm dictionary by Haruhiko Okumura (PAF01002@niftyserve.or.jp)
6  */
7 
8 char rcsid_line[] = "$Id: line.c,v 1.4 1995/11/23 16:28:39 yav Exp $";
9 
10 #define DOT(x,y) (*pp++ = (x), *pp++ = (y))
11 
12 /* return: number of points */
line(pp,x1,y1,x2,y2)13 int line(pp, x1, y1, x2, y2)
14      int *pp;
15      int x1;
16      int y1;
17      int x2;
18      int y2;
19 {
20   int dx, dy, s, ystep, xstep, cnt;
21 
22   dx = abs(x2 - x1);  dy = abs(y2 - y1);
23   xstep = (x1 < x2) ? 1: -1;
24   ystep = (y1 < y2) ? 1: -1;
25   if (dx > dy) {
26     s = dx >> 1;
27     cnt = dx+1;
28     do {
29       DOT(x1, y1);
30       if ((s -= dy) < 0) {
31 	s += dx;  y1 += ystep;
32       };
33       x1 += xstep;
34     } while (--cnt);
35     return dx+1;
36   } else {
37     s = dy >> 1;
38     cnt = dy+1;
39     do {
40       DOT(x1, y1);
41       if ((s -= dx) < 0) {
42 	s += dy;  x1 += xstep;
43       }
44       y1 += ystep;
45     } while (--cnt);
46     return dy+1;
47   }
48 }
49 
50 /* End of file */
51