home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / tech / pcbsrcs2 / line.c < prev    next >
C/C++ Source or Header  |  1991-02-07  |  5KB  |  169 lines

  1. /* WARNING: the code below assumes 640x350 16-color ega */
  2.  
  3. /* 0=black    1=blue        2=green        3=light blue        */
  4. /* 4=red    5=purple    6=brown        7=grey            */
  5. /* 8=black    9=bright blue    A=bright green    B=bright light blue    */
  6. /* C=scarlet    D=purple    E=yellow    F=white            */
  7.  
  8. #define T    0x9    /* trace color; bright blue    */
  9.  
  10. /* screen limits; we don't draw on the very edge pixels */
  11. #define MINHORZ    0    /* left-most pixel    */
  12. #define MAXHORZ    639    /* right-most pixel    */
  13. #define MINVERT    0    /* top-most pixel    */
  14. #define MAXVERT    349    /* bottom-most pixel    */
  15.  
  16. extern void Dot( int, int, int );
  17.  
  18. void Line( int, int, int, int, int );
  19.  
  20. /*
  21. ** it is assumed that at least some part of the line must be drawn.
  22. ** whoever calls Line should ensure that this is the case, or refrain
  23. ** from making the call. otherwise, lots of time can be wasted on a
  24. ** line that is not even in the viewing window.
  25. */
  26.  
  27. /*
  28. ** NOTE: the screen origin is in the upper left-hand corner, not in
  29. ** the lower left-hand corner, as you would normally think of an
  30. ** (x,y) graph.
  31. */
  32.  
  33. void Line ( r1, c1, r2, c2, endpt ) /* draw a line between two points */
  34.     int r1, c1, r2, c2, endpt; /* pixel rows and columns */
  35.     {
  36.     int i, j, rdelta, cdelta, acc, frac, aux;
  37.  
  38. /*printf("Line(r1=%d,c1=%d,r2=%d,c2=%d,endpt=%d)\n",r1,c1,r2,c2,endpt);*/
  39.     if (endpt) { /* draw the two endpoints */
  40.         if (r1 >= MINVERT && r1 <= MAXVERT
  41.             && c1 >= MINHORZ && c1 <= MAXHORZ)
  42.             Dot( T, r1, c1 );
  43.         if (r2 >= MINVERT && r2 <= MAXVERT
  44.             && c2 >= MINHORZ && c2 <= MAXHORZ)
  45.             Dot( T, r2, c2 );
  46.         }
  47.     /* get leftmost endpoint into (r1,c1); swap if necessary */
  48.     if (c2 < c1) {
  49.         aux = c1;  c1 = c2;  c2 = aux;
  50.         aux = r1;  r1 = r2;  r2 = aux;
  51.         }
  52.     if (c1 == c2) { /* vertical line */
  53.         if (c1 >= MINHORZ && c1 <= MAXHORZ) {
  54.             if (r1 < r2) { /* (r1,c1) is on top */
  55.                 for (i = r1+1; i < r2; i++)
  56.                     if (i >= MINVERT && i <= MAXVERT)
  57.                         Dot( T, i, c1 );
  58.                 }
  59.             else { /* (r2,c2) is on top */
  60.                 for (i = r1-1; i > r2; i--)
  61.                     if (i >= MINVERT && i <= MAXVERT)
  62.                         Dot( T, i, c1 );
  63.                 }
  64.             }
  65.         }
  66.     else if (r1 == r2) { /* horizontal line */
  67.         if (r1 >= MINVERT && r1 <= MAXVERT)
  68.             for (i = c1+1; i < c2; i++)
  69.                 if (i >= MINHORZ && i <= MAXHORZ)
  70.                     Dot( T, r1, i );
  71.         }
  72.     else { /* line running from left to right: (r1,c1) -> (r2,c2) */
  73.         rdelta = r2-r1; /* positive or negative */
  74.         cdelta = c2-c1; /* always non-negative */
  75.         if (rdelta == cdelta) { /* line has slope of -1 */
  76.             for (i = r1+1, j = c1+1; i < r2 && j < c2; i++, j++)
  77.                 if (i >= MINVERT && i <= MAXVERT
  78.                     && j >= MINHORZ && j <= MAXHORZ)
  79.                     Dot( T, i, j );
  80.             }
  81.         else if (-rdelta == cdelta) { /* line has slope of +1 */
  82.             for (i = r1-1, j = c1+1; i > r2 && j < c2; i--, j++)
  83.                 if (i >= MINVERT && i <= MAXVERT
  84.                     && j >= MINHORZ && j <= MAXHORZ)
  85.                     Dot( T, i, j );
  86.             }
  87.         /*
  88.         ** find out which sector the line is in:
  89.         **
  90.         ** MINVERT
  91.         **        (rdelta negative)
  92.         **    |      /
  93.         **    | 1  /
  94.         **    |  /
  95.         **   \|/   2
  96.         **   -*-------
  97.         **   /|\   3
  98.         **    |  \
  99.         **    | 4  \
  100.         **    |      \
  101.         **        (rdelta positive)
  102.         ** MAXVERT
  103.         */
  104.         else if (rdelta < 0) { /* positive slope */
  105.             rdelta = -rdelta;
  106.             if (rdelta > cdelta) { /* sector 1 */
  107.                 for (i = r1-1, acc = 0, frac = cdelta;
  108.                     i > r2; i--, frac += cdelta) {
  109.                     if (frac >= rdelta) {
  110.                         acc++;
  111.                         frac -= rdelta;
  112.                         }
  113.                     aux = (frac+frac >= rdelta) ? 1 : 0;
  114.                     if (i >= MINVERT && i <= MAXVERT
  115.                         && (j = c1+acc+aux) >= MINHORZ
  116.                         && j <= MAXHORZ)
  117.                         Dot( T, i, j );
  118.                     }
  119.                 }
  120.             else { /* sector 2 */
  121.                 for (j = c1+1, acc = 0, frac = rdelta;
  122.                     j < c2; j++, frac += rdelta) {
  123.                     if (frac >= cdelta) {
  124.                         acc++;
  125.                         frac -= cdelta;
  126.                         }
  127.                     aux = (frac+frac >= cdelta) ? 1 : 0;
  128.                     if ((i = r1-acc-aux) >= MINVERT
  129.                         && i <= MAXVERT
  130.                         && j >= MINHORZ
  131.                         && j <= MAXHORZ)
  132.                         Dot( T, i, j );
  133.                     }
  134.                 }
  135.             }
  136.         else { /* negative slope */
  137.             if (rdelta < cdelta) { /* sector 3 */
  138.                 for (j = c1+1, acc = 0, frac = rdelta;
  139.                     j < c2; j++, frac += rdelta) {
  140.                     if (frac >= cdelta) {
  141.                         acc++;
  142.                         frac -= cdelta;
  143.                         }
  144.                     aux = (frac+frac >= cdelta) ? 1 : 0;
  145.                     if ((i = r1+acc+aux) >= MINVERT
  146.                         && i <= MAXVERT
  147.                         && j >= MINHORZ
  148.                         && j <= MAXHORZ)
  149.                         Dot( T, i, j );
  150.                     }
  151.                 }
  152.             else { /* sector 4 */
  153.                 for (i = r1+1, acc = 0, frac = cdelta;
  154.                     i < r2; i++, frac += cdelta) {
  155.                     if (frac >= rdelta) {
  156.                         acc++;
  157.                         frac -= rdelta;
  158.                         }
  159.                     aux = (frac+frac >= rdelta) ? 1 : 0;
  160.                     if (i >= MINVERT && i <= MAXVERT
  161.                         && (j = c1+acc+aux) >= MINHORZ
  162.                         && j <= MAXHORZ)
  163.                         Dot( T, i, j );
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.     }
  169.