home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / lemming / part02 / lemvec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-05  |  4.2 KB  |  180 lines

  1. /*
  2.  * lemvec.c - line to point scan conversion
  3.  *
  4.  * copyright (c) by Alan W. Paeth, 1987. All rights reserved.
  5.  */
  6.  
  7. /*
  8.  *
  9.  * Programmed by Alan Paeth, University of Waterloo, January, 1984
  10.  *
  11.  * This code rasterizes vectors. A call of the form:
  12.  *
  13.  * drawline(x0, y0, x1, y1, wid, val, emph)
  14.  *
  15.  * with val the output color, and emph one of EMPHITAL, EMPHBOLD, (otherwise)
  16.  * generates a vector of specified which is dotted, dashed, or plain,
  17.  * respectively.
  18.  *
  19.  * output pixels are set by calls to:
  20.  *
  21.  * setpixelrun(x, y, wid, val, parity)
  22.  *
  23.  * which should set pixels (x,y) through (x+wid-1,y) to color (val) if
  24.  * parity is 0 (as it is at endpoints and some intermediate locations).
  25.  *
  26.  * viewport clipping is done (using integer math) to the dimensions specified
  27.  * in the globals "screenw" and "screenh"
  28.  *
  29.  */
  30.  
  31. #include "lem.h"
  32.  
  33. #define Nextflag { mask>>=1; if (++flag >= 16) { flag = 0; mask = emph; } }
  34.  
  35. #define SOLID  0xffffffff    /* only solid should have sign bit set */
  36. #define DASHES 0x00ff00ff
  37. #define DOTS   0x03030303
  38.  
  39. #define ONE        0x4000
  40. #define POINT_FIVE    0x2000
  41. #define SCALEUP    14
  42.  
  43. #define TOPFLAG 8
  44. #define BOTTOMFLAG 4
  45. #define LEFTFLAG 2
  46. #define RIGHTFLAG 1
  47.  
  48. code(x, y)
  49.     float x, y;
  50.     {
  51.     int c = 0;
  52.     if (x < 0) c |= LEFTFLAG; else if (x >= screenw) c |= RIGHTFLAG;
  53.     if (y < 0) c |= BOTTOMFLAG; else if (y >= screenh) c |= TOPFLAG;
  54.     return c;
  55.     }
  56.  
  57. drawline(x1, y1, x2, y2, wid, val, emph) /* generic brand clipped line code */
  58.     {
  59.     int c, c1, c2;
  60.     long x, y;
  61.  
  62.     if ((x1==x2) && (y1==y2)) return;    /* no motion -- fast return */
  63.  
  64.     c1 = code((float)(x1), (float)(y1));
  65.     c2 = code((float)(x2), (float)(y2));
  66.  
  67.     while (c1 || c2)
  68.     {
  69.     if (c1 & c2) return;    /* bitwise AND, not statement AND */
  70.     c = c1 ? c1 : c2;
  71.     
  72.     if (c & LEFTFLAG)
  73.         y = y1 + ((y2 - y1) * ( (x = 0) - x1)) / (x2 - x1);
  74.     else if (c & RIGHTFLAG)
  75.         y = y1 + ((y2 - y1) * ( (x = screenw) - x1)) / (x2 - x1);
  76.     else if (c & TOPFLAG)    
  77.         x = x1 + ((x2 - x1) * ( (y = screenh) - y1)) / (y2 - y1);
  78.     else if (c & BOTTOMFLAG)    
  79.         x = x1 + ((x2 - x1) * ( (y = 0) - y1)) / (y2 - y1);
  80.     
  81.     if (c == c1)
  82.         {
  83.         x1 = x;
  84.         y1 = y;
  85.         c1 = code(x, y);
  86.         }
  87.     else
  88.         {
  89.         x2 = x;
  90.         y2 = y;
  91.         c2 = code(x, y);
  92.         }
  93.     }
  94.     fastdrawline(x1, y1, x2, y2, wid, val, emph);
  95.     }
  96.  
  97. fastdrawline(x1, y1, x2, y2, wid, val, emph)
  98.  
  99. /*
  100.  *
  101.  *  Draws a line of pixels=val from (x1, y1) to (x2, y2) very fast.
  102.  *
  103.  *  This is algorithm A1 from "Filtering Edges for Grey-Scale Displays" by
  104.  *  Gupta & Sproull (Computer Graphics 15,3 August 1981). No anti-aliasing
  105.  *  is being done here. However the variable 'v' can be *  used to turn on
  106.  *  pixels to the left and right (above and below) the line since v is
  107.  *  horizontal distance from the center of the line to the pixel at (x, y).
  108.  *
  109.  *  Last Hacked by: Alan Paeth
  110.  */
  111.  
  112.     int x1, y1, x2, y2, val, emph;
  113.     {
  114.     register    x, y;            /* current position in line */
  115.     int        incr;            /* y increment = + or - 1 */
  116.     int        dx, dy;            /* change in x, y */
  117.     int        m;            /* slope of line ( * 2 ** 14 ) */
  118.     int        s;            /* threshold for diagonal move */
  119.     register    v;            /* dist from line to pixel */
  120.     int        mm1;            /* m - (1 << 9) */
  121.     int        mask;
  122.     int        flag;
  123.  
  124.     flag = 0;
  125.     emph = (emph == EMPHITAL) ? DOTS : ((emph == EMPHBOLD) ? DASHES : SOLID);
  126.     mask = emph;
  127.  
  128.     if (y2 > y1)            /* make y1 > y2 by symmetries */
  129.     {
  130.     x = x1;   x1 = x2;   x2 = x;
  131.     y = y1;   y1 = y2;   y2 = y;
  132.     }
  133.  
  134.     incr = 1;
  135.     dy   = y1 - y2;
  136.  
  137.     if ((dx = x2 - x1) < 0)
  138.     {
  139.     dx   = -dx;
  140.     incr = -1;
  141.     }
  142.  
  143.     v = 0;
  144.     if (dx > dy)
  145.     {
  146.     m = ((long)dy << SCALEUP) / (long)dx;
  147.     s = POINT_FIVE - m;
  148.     mm1 = m - ONE;
  149.     y = y1;
  150.  
  151.     for( x = x1; x != x2; x += incr )
  152.         {
  153.         if (mask & 0x1) setpixelrunv(x, y, wid, val, flag & 0x1);
  154.         Nextflag;
  155.         if( v >= s ) { --y; v += mm1; }    /* diagonal move */
  156.         else v += m;            /* horizontal move */
  157.         }
  158.     setpixelrunv(x, y, wid, val, 0);
  159.     }
  160.     else
  161.     {
  162.     if( dy > 0 )
  163.         {
  164.         m = ((long)dx << SCALEUP) / (long)dy;
  165.         s = POINT_FIVE - m;
  166.         mm1 = m - ONE;
  167.         }
  168.     x = x1;
  169.  
  170.     for( y = y1; y > y2; --y )
  171.         {
  172.         if (mask & 0x1) setpixelrunh(x, y, wid, val, flag&0x1);
  173.         Nextflag;
  174.         if ( v >= s ) { x += incr; v += mm1; }    /* diagonal move */
  175.         else v += m;                /* vertical move */
  176.         }
  177.     setpixelrunh(x, y, wid, val, 0);
  178.     }
  179.     }
  180.