home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / VOGLE / DRIVERS / IBMPC / PC_FILL.C < prev    next >
C/C++ Source or Header  |  2000-02-11  |  3KB  |  166 lines

  1. #include "vogle.h"
  2.  
  3. void
  4. pc_fill(n, x, y)
  5.     int    n, *x, *y;
  6. {
  7.     register    int    i;
  8.     int        tx[3], ty[3], xstart, xend;
  9.  
  10.     /*
  11.      * If it's a rectangle then things are even easier....
  12.      */
  13.     if (n == 5 && x[0] == x[3] && x[1] == x[2] &&
  14.         y[0] == y[1] && y[2] == y[3]) {
  15.         int    low, high;
  16.         xstart = x[0];
  17.         xend = x[1];
  18.         low = y[0];
  19.         high = y[2];
  20.         if (low >= high) {
  21.             low = high;
  22.             high = y[0];
  23.         }
  24.         for (i = low; i <= high; i++) {
  25.             vdevice.cpVx = xstart;
  26.             vdevice.cpVy = i;
  27.             (*vdevice.dev.Vdraw)(xend, i);
  28.         }
  29.     } else {
  30.  
  31.         for (i = 0; i < n - 2; i++) {
  32.             tx[0] = x[0];
  33.             ty[0] = y[0];
  34.             tx[1] = x[i+1];
  35.             ty[1] = y[i+1];
  36.             tx[2] = x[i+2];
  37.             ty[2] = y[i+2];
  38.             fill_tri(tx, ty);
  39.         }
  40.     }
  41. }
  42.  
  43. #define ABS(x)        ((x) > 0 ? (x) : -(x))
  44. #define SIGN(x)        ((x) > 0 ? 1 : ((x) == 0 ? 0 : (-1)))
  45. #define    MAX(x,y)    ((x) > (y) ? (x) : (y))
  46.  
  47. fill_tri(tx, ty)
  48.     int tx[3], ty[3];
  49. {
  50.     register int y;
  51.     register int    dx1, dx2, dy1, dy2, inc1, inc2, sx1, sx2, sy1,
  52.     xl, xr, x1, x2, ix1, ix2, iy1, iy2, doit, doit2, y1, y2;
  53.  
  54.     /* 
  55.      * Order in y values
  56.      */
  57.     y1 = 0;
  58.     while (!y1) {
  59.         y1 = 1;
  60.         for (y2 = 0; y2 < 2; y2++)
  61.             if (ty[y2] > ty[y2+1] || (ty[y2] == ty[y2+1] && tx[y2] < tx[y2+1])) {
  62.                 x1 = ty[y2];
  63.                 ty[y2] = ty[y2+1];
  64.                 ty[y2+1] = x1;
  65.                 x1 = tx[y2];
  66.                 tx[y2] = tx[y2+1];
  67.                 tx[y2+1] = x1;
  68.                 y1 = 0;
  69.             }
  70.     }
  71.      dx1 = tx[1] - tx[0];
  72.     dx2 = tx[2] - tx[0];
  73.     dy1 = ty[1] - ty[0];
  74.     dy2 = ty[2] - ty[0];
  75.     sx1 = SIGN(dx1);
  76.     sx2 = SIGN(dx2);
  77.     sy1 = SIGN(dy1);
  78.     ix1 = ABS(dx1);
  79.     ix2 = ABS(dx2);
  80.     iy1 = ABS(dy1);
  81.     iy2 = ABS(dy2);
  82.     inc1 = MAX(ix1, iy1);
  83.     inc2 = MAX(ix2, iy2);
  84.  
  85.     x1 = x2 = y1 = y2 = 0;
  86.     xl = xr = tx[0];
  87.     y = ty[0];
  88.  
  89.     while (y != ty[1]) {
  90.         doit = 0;
  91.         doit2 = 0;
  92.         while (!doit) {        /* Wait until y changes */
  93.             x1 += ix1;
  94.             y1 += iy1;
  95.             if (x1 > inc1) {
  96.                 x1 -= inc1;
  97.                 xl += sx1;
  98.             }
  99.             if (y1 > inc1) {
  100.                 y1 -= inc1;
  101.                 y += sy1;
  102.                 doit = 1;
  103.             }
  104.         }
  105.         while (!doit2) {    /* Wait until y changes */
  106.             x2 += ix2;
  107.             y2 += iy2;
  108.             if (x2 > inc2) {
  109.                 x2 -= inc2;
  110.                 xr += sx2;
  111.             }
  112.             if (y2 > inc2) {
  113.                 y2 -= inc2;
  114.                 doit2 = 1;
  115.             }
  116.         }
  117.         
  118.         vdevice.cpVx = xl;
  119.         vdevice.cpVy = y;
  120.         (*vdevice.dev.Vdraw)(xr, y);
  121.     }
  122.     dx1 = tx[2] - tx[1];
  123.     dy1 = ty[2] - ty[1];
  124.     sx1 = SIGN(dx1);
  125.     sy1 = SIGN(dy1);
  126.     ix1 = ABS(dx1);
  127.     iy1 = ABS(dy1);
  128.     inc1 = MAX(ix1, iy1);
  129.     xl = tx[1];
  130.     x1 = 0;
  131.     while (y != ty[2]) {
  132.         doit = 0;
  133.         doit2 = 0;
  134.         while (!doit) {        /* Wait until y changes */
  135.             x1 += ix1;
  136.             y1 += iy1;
  137.             if (x1 > inc1) {
  138.                 x1 -= inc1;
  139.                 xl += sx1;
  140.             }
  141.             if (y1 > inc1) {
  142.                 y1 -= inc1;
  143.                 y += sy1; 
  144.                 doit = 1;
  145.             }
  146.         }
  147.         while (!doit2) {    /* Wait until y changes */
  148.             x2 += ix2;
  149.             y2 += iy2;
  150.             if (x2 > inc2) {
  151.                 x2 -= inc2;
  152.                 xr += sx2;
  153.             }
  154.             if (y2 > inc2) {
  155.                 y2 -= inc2;
  156.                 doit2 = 1;
  157.             }
  158.         }
  159.         vdevice.cpVx = xl;
  160.         vdevice.cpVy = y;
  161.         (*vdevice.dev.Vdraw)(xr, y);
  162.     }
  163.  
  164.     return(0);
  165. }
  166.