home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 514b.lha / line+fill / fillopt < prev    next >
Text File  |  1991-06-08  |  4KB  |  134 lines

  1. Here's some newer (optimized) code to play with...
  2. (Just the C front end to the assembly code just reposted)
  3.  
  4.   John
  5.  
  6. /* fill.c, processor poly filler   */
  7. /* Copyright (c) 1991 John Schultz */
  8. /* This is the latest version of the C interface to my processor
  9.    polygon fill code.
  10. */
  11.  
  12. typedef struct shortpoint2d {
  13.   short x,y;
  14. } shortpoint2d;
  15.  
  16. #define WIDTH  320
  17. #define HEIGHT 200
  18.  
  19. static short xmin[HEIGHT+1], xmax[HEIGHT+1];
  20. static short * xt;
  21.  
  22. /******************* COUNTERCLOCKWISE POLYGONS ********************/
  23. /**************** (This version does clockwise too) ***************/
  24.  
  25. void drawpolyc(shortpoint2d * vp,
  26.                char * p,    /* PLANEPTR */
  27.                short count,
  28.                short color){
  29. shortpoint2d * tmax, * tmin;
  30. shortpoint2d * tp, * tpn;
  31. shortpoint2d * last;
  32. long cmp;
  33. long orient;
  34.  
  35. /* Get last point */
  36.   last = &vp[count-1];
  37.  
  38. /* Cross product to check for check for line case */
  39.   if (count > 3) { /* Use Newell method */
  40.     orient = 0;
  41.     tp = vp; /* Point to first point */
  42.     while (1) {
  43.       if (tp == last)
  44.         tpn = vp; /* Set next pointer to first point */
  45.       else
  46.         tpn = tp + 1; /* point to next. Compiler does: tp + sizeof(*tp) */
  47.       orient += (tp->x - tpn->x)*(tp->y + tpn->y);
  48.       if (tpn == vp) break; /* Come full circle */
  49.       tp = tpn; /* go to next */
  50.     } /* while */
  51.   } else { /* Simple cross-product: triangle case */
  52.     orient = (vp[1].x - vp[0].x)*(vp[2].y - vp[0].y) - 
  53.              (vp[1].y - vp[0].y)*(vp[2].x - vp[0].x);
  54.   } /* if count */
  55.  
  56.   if (orient == 0L) { /* Collinear: draw a line */
  57. /* Find two points that aren't equal */
  58.     cmp = *(long *)vp; /* First point */
  59.     tp = vp + 1; /* Point to next (Compiler does: vp + sizeof(*vp) */
  60.     while (1) {
  61.       if ((cmp != (*(long *)tp)) || (tp == last)) break;
  62.       tp++;      
  63.     } /* while */
  64.     drawline(p,vp->x,vp->y,tp->x,tp->y,color);
  65.     return;
  66.   } /* if line case */
  67.  
  68. /* Find miny,maxy */
  69.   tmin = vp;   /* Point at first */
  70.   tmax = vp;   /* "            " */
  71.   tp = vp + 1; /* Point to next (Compiler does: vp + sizeof(*vp) */
  72.  
  73.   while (1) {
  74.     if (tp->y < tmin->y) {
  75.       tmin = tp;
  76.     } else if (tp->y == tmin->y) {
  77.       if (tp->x >= tmax->x) {
  78.         tmin = tp;
  79.       } /* if tp->x */
  80.     } /* if tp->y */
  81.     if (tp->y > tmax->y) {  /* MaxY */
  82.       tmax = tp;
  83.     } else if (tp->y == tmax->y) {
  84.       if (tp->x < tmax->x) { /* Get maxY, minX */
  85.         tmax = tp;
  86.       } /* if tp->x */
  87.     } /* if tmaxy */
  88.     if (tp == last) break;
  89.     tp++;
  90.   };
  91.  
  92.   if (orient < 0) {
  93.  
  94. /* Fill tables */
  95.   tp = tmin; /* Temp point starts at miny */
  96.   xt = xmin; /* Fill xmin table first */
  97.   while (1) {
  98.     if (tp == last)
  99.       tpn = vp; /* Set to first point */
  100.     else
  101.       tpn = tp + 1; /* Next. Compiler does: tp + sizeof(*tp) */
  102.     fillline68k(tp->x,tp->y,tpn->x,tpn->y,xt);
  103.     if (tpn == tmin) break; /* Come full circle, quit */
  104.     if (tpn == tmax) xt = xmax; /* Fill max table */
  105.     tp = tpn; /* Go to next */
  106.   } /* while */
  107.  
  108.   } else {
  109.  
  110. /* Fill tables */
  111.   tp = tmin; /* Temp point starts at miny */
  112.   xt = xmax; /* Fill xmax table first */
  113.   while (1) {
  114.     if (tp == last)
  115.       tpn = vp; /* Set to first point */
  116.     else
  117.       tpn = tp + 1; /* Next. Compiler does: tp + sizeof(*tp) */
  118.     fillline68k(tp->x,tp->y,tpn->x,tpn->y,xt);
  119.     if (tpn == tmin) break; /* Come full circle, quit */
  120.     if (tpn == tmax) xt = xmin; /* Fill min table */
  121.     tp = tpn; /* Go to next */
  122.   } /* while */
  123.  
  124.   } /* if orient */
  125.  
  126. /* Draw polygon */
  127.   scanconvpix(p,xmin,xmax,tmin->y,tmax->y,color);
  128.  
  129. } /* drawpolyc */
  130.  
  131. /* end fill.c */
  132.  
  133.  
  134.