home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / C-ASM_VI.ARJ / PROGC.ZIP / PROGC082.C < prev    next >
Text File  |  1988-05-16  |  5KB  |  128 lines

  1.  
  2. /************************************************************************/
  3. /* Fill a base triangle using scan-line function                        */
  4. /* and then fill a general triangle (using triangle fill routine)       */
  5. /************************************************************************/
  6.  
  7. fill_triag()
  8.         {
  9.  
  10.         /* Fill a base line triangle                                    */
  11.  
  12.         int     x0 = 100,x1 = 0, x2 = 300, y0 = 10, y1 = 100, y2 = 100;
  13.         int     y, color = 15, start, end;
  14.         for (y = y0; y <= y1; y++)      /* Loop over raster lines       */
  15.                 {                       /* Compute intercepts           */
  16.                 start = x0 + ((y - y0)*(x1 - x0))/(y1 - y0);
  17.                 end   = x0 + ((y - y0)*(x2 - x0))/(y2 - y0);
  18.                 scanline(start,end,y,color);    /* Fill next section    */
  19.                 }
  20.  
  21.         /* Fill a general triangle                                      */
  22.  
  23.         triangle(400,100, 500,0, 630,200, color);
  24.         }
  25.  
  26. /************************************************************************/
  27. /*                                                                      */
  28. /*  triangle(x0,y0,x1,y1,x2,y2,color) - Fill a general triangle with    */
  29. /*             color 'color'.  This routine will call the scanline      */
  30. /*             procedure to fill scanlines between two edges of the     */
  31. /*             triangle.                                                */
  32. /*                                                                      */
  33. /*             First vertices are ordered so that vertex 'a' is the     */
  34. /*             left most top vertex, and 'b' is the left most bottom    */
  35. /*             vertex.  Triangle is than split into two 'baseline'      */
  36. /*             triangles, and each triangle is filled separately.       */
  37. /*             One triangle is an upward pointing arrow and the second  */
  38. /*             Triangle is a downward pointing arrow.                   */
  39. /*                                                                      */
  40. /************************************************************************/
  41.  
  42. triangle(x0,y0, x1,y1, x2,y2, color)
  43. int     x0,y0, x1,y1, x2,y2,color;
  44.         {
  45.         int     x[4],y[4],a,b,c,temp;
  46.         x[0] = x0;
  47.         x[1] = x1;
  48.         x[2] = x2;
  49.         y[0] = y0;
  50.         y[1] = y1;
  51.         y[2] = y2;
  52.  
  53.         /* Order the vertices */
  54.         a = 0;                                  /* Get lowest left in a */
  55.         if (y[0] > y[1]) a = 1;
  56.         if (y[a] > y[2]) a = 2;
  57.         b = (a+1)%3;
  58.         c = (b+1)%3;
  59.         if (y[a] == y[b] && x[a] > x[b]) a = b;
  60.         if (y[a] == y[c] && x[a] > x[c]) a = c;
  61.         b = (a+1)%3;
  62.         c = (b+1)%3;
  63.         if (y[b] < y[c])                        /* Get highest left in b*/
  64.                 {
  65.                 temp = b;
  66.                 b = c;
  67.                 c = temp;
  68.                 }
  69.         if (y[b] == y[c] && x[b] > x[c])
  70.                 {
  71.                 temp = b;
  72.                 b = c;
  73.                 c = temp;
  74.                 }
  75.         /* fill 'base' triangles */
  76.         if (y[a] == y[b])                       /* Fill degenerates     */
  77.                 {
  78.                 scanline(x[a],x[b],y[b],color);
  79.                 scanline(x[a],x[c],y[c],color);
  80.                 }
  81.         else if (y[a] == y[c])                  /* Fill down arrow      */
  82.                 fill_dw(x[a],y[a],x[b],y[b],x[c],y[c],x[b],y[b],y[c],color);
  83.         else if (y[b] == y[c])                  /* Fill up arrow        */
  84.                 fill_up(x[a],y[a],x[b],y[b],x[a],y[a],x[c],y[c],y[c],color);
  85.         else
  86.                 {                               /* Split into two bases */
  87.                 y[3] = y[c];                    /* one up and one down  */
  88.                 x[3] = x[a] + ((y[3] - y[a])*(long int)(x[b] - x[a]))/(y[b] - y[a]);
  89.                 if (x[3] < x[c])                /* 'c' is to the right  */
  90.                     {
  91.                     fill_up(x[a],y[a],x[b],y[b],x[a],y[a],x[c],y[c],y[c],color);
  92.                     fill_dw(x[a],y[a],x[b],y[b],x[c],y[c],x[b],y[b],y[c],color);
  93.                     }
  94.                 else                            /* 'c' is to the left   */
  95.                     {
  96.                     fill_up(x[a],y[a],x[c],y[c],x[a],y[a],x[b],y[b],y[c],color);
  97.                     fill_dw(x[c],y[c],x[b],y[b],x[a],y[a],x[b],y[b],y[c],color);
  98.                     }
  99.                 }
  100.         }
  101.  
  102. fill_up(ax,ay, bx,by, cx,cy, dx,dy,y0,color)    /* Fill up arrow        */
  103. int     ax,ay,bx,by,cx,cy,dx,dy,y0,color;
  104.         {
  105.         int    dyab,dycd,y,d1,d2;
  106.         dycd = dy - cy;
  107.         dyab = by - ay;
  108.         for (y = ay; y <= y0; y++)
  109.                 {
  110.                 d1 = ((y - ay)*(long)(bx - ax))/dyab;
  111.                 d2 = ((y - cy)*(long)(dx - cx))/dycd;
  112.                 scanline(ax+d1, cx+d2, y,color);
  113.                 }
  114.         }
  115. fill_dw(ax,ay, bx,by, cx,cy, dx,dy,y0,color)    /* Fill down arrow      */
  116. int     ax,ay,bx,by,cx,cy,dx,dy,y0,color;
  117.         {
  118.         int    dyab,dycd,y,d1,d2;
  119.         dycd = dy - cy;
  120.         dyab = by - ay;
  121.         for (y = dy; y >= y0; y--)
  122.                 {
  123.                 d1 = ((y - ay)*(long)(bx - ax))/dyab;
  124.                 d2 = ((y - cy)*(long)(dx - cx))/dycd;
  125.                 scanline(ax+d1, cx+d2, y, color);
  126.                 }
  127.         }
  128.