home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1986 / 02 / rollins.feb < prev    next >
Text File  |  1986-02-27  |  4KB  |  109 lines

  1. /* ******************************************************************
  2.   LINE.C
  3.   Copyright (C) 1984 by Dan Rollins
  4.   Uses incremental algorithm and fast ASM plotdot
  5.   Permission is granted to use this for any purpose whatsoever.
  6. ******************************************************************* */
  7.  
  8. _main(arglin)  /* this just skips the 'main' function altogether */
  9. char *arglin;  /* points to the DOS command line */
  10. {
  11.   int x,y;
  12.  
  13.   dmode(1);  /* enter text mode first (so screen will clear) */
  14.   dmode(4);  /* mid-res color graphics mode */
  15.  
  16.   /* test the line algorithm by drawing in all directions, colors */
  17.  
  18.   for (x=0; x<320; x+=4)  plotline(160,100,  x,  0, 3);
  19.   for (y=0; y<200; y+=4)  plotline(160,100,319,  y, 2);
  20.   for (x=319; x>=0; x-=4) plotline(160,100,  x,199, 1);
  21.   for (y=199; y>=0; y-=4) plotline(160,100,  0,  y, 2);
  22.  
  23.   getch(); /* pause till key is pressed */
  24.   dmode(2);  /* re-enter text mode */
  25. }
  26.  
  27. /* ******************************************************************
  28. PLOTLINE(x1,y1,x2,y2,color)
  29. draws a line from (x1,y1) to (x2,y2) in specified color (0 to 3)
  30. calls 'plotdot' (a fast, mid-resolution pixel-plotting routine)
  31. ******************************************************************* */
  32. plotline(x1,y1,x2,y2,color)
  33. int x1,y1;    /* starting point */
  34. int x2,y2;    /* ending point */
  35. int color;
  36. {
  37.                                  /* use static variables for fastest access */
  38.   static int lg_delta, sh_delta; /* distance of long, short axis */
  39.   static int lg_step, sh_step;   /* 0, 1 or -1 */
  40.   static int cycle;              /* decision variable */
  41.   static int temp;               /* swapping variable */
  42.  
  43.   lg_delta = x2-x1;              /* get travel along X axis */
  44.   if (lg_delta >= 0)
  45.     lg_step = 1;
  46.   else {
  47.     lg_delta = -lg_delta;        /* get absolute value */
  48.     lg_step = -1;                /* reverse direction */
  49.   }
  50.  
  51.   sh_delta = y2-y1;              /* get travel along Y axis */
  52.   if (sh_delta >= 0)
  53.     sh_step = 1;
  54.   else {
  55.     sh_delta = -sh_delta;        /* get absolute value */
  56.     sh_step = -1;                /* reverse direction */
  57.   }
  58.  
  59.   if (sh_delta > lg_delta) {     /* if Y axis is longer, swap axes */
  60.     cycle = sh_delta >> 1;
  61.     temp = lg_delta; lg_delta = sh_delta; sh_delta = temp;
  62.     temp = lg_step; lg_step = sh_step; sh_step = temp;
  63.  
  64.     while (y1 != y2) {         /* loop for "vertical" line */
  65.       plotpix(x1,y1,color);
  66.       y1 += lg_step;           /* always bump line pointer */
  67.       cycle += sh_delta;       /* bump decision variable */
  68.       if (cycle >= lg_delta) { /* past decision threshold? */
  69.         cycle -= lg_delta;     /* reset for next decison cycle */
  70.         x1 += sh_step;         /* bump column pointer */
  71.       }
  72.     }
  73.   }
  74.   else {                       /* X axis is longer, so don't swap */
  75.     cycle = lg_delta >> 1;
  76.     while (x1 != x2) {         /* loop for "horizontal" line */
  77.       plotpix(x1,y1,color);
  78.       x1 += lg_step;
  79.       cycle += sh_delta;
  80.       if (cycle >= lg_delta) {
  81.         cycle -= lg_delta;
  82.         y1 += sh_step;
  83.       }
  84.     } /* end of while */
  85.   } /* end of else (for axis swap) */
  86. } /* end of plotline */
  87.  
  88.  
  89. /* ******************************************************************
  90. DMODE(mode)
  91. sets the display mode
  92. mode is:  0 = bw    40x25 text     4 = color 320x200 graphics
  93.           1 = color 40x25 text     5 = bw    320x200 graphics
  94.           2 = bw    80x25 text     6 = bw    640x200 graphics
  95.           3 = color 80x25 text
  96. ******************************************************************* */
  97. dmode(m)
  98. int m;
  99. {
  100.   struct XREGS{ int ax,bx,cx,dx; } regbuf;
  101.   regbuf.ax = m;    /* AH = 0, AL = mode */
  102.   int86(0x10,®buf,®buf);
  103. }
  104.  
  105.     /* AH = 0, AL = mode */
  106.   int86(0x10,®buf,®buf);
  107. }
  108.  
  109.