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 >
Wrap
Text File
|
1988-05-16
|
5KB
|
128 lines
/************************************************************************/
/* Fill a base triangle using scan-line function */
/* and then fill a general triangle (using triangle fill routine) */
/************************************************************************/
fill_triag()
{
/* Fill a base line triangle */
int x0 = 100,x1 = 0, x2 = 300, y0 = 10, y1 = 100, y2 = 100;
int y, color = 15, start, end;
for (y = y0; y <= y1; y++) /* Loop over raster lines */
{ /* Compute intercepts */
start = x0 + ((y - y0)*(x1 - x0))/(y1 - y0);
end = x0 + ((y - y0)*(x2 - x0))/(y2 - y0);
scanline(start,end,y,color); /* Fill next section */
}
/* Fill a general triangle */
triangle(400,100, 500,0, 630,200, color);
}
/************************************************************************/
/* */
/* triangle(x0,y0,x1,y1,x2,y2,color) - Fill a general triangle with */
/* color 'color'. This routine will call the scanline */
/* procedure to fill scanlines between two edges of the */
/* triangle. */
/* */
/* First vertices are ordered so that vertex 'a' is the */
/* left most top vertex, and 'b' is the left most bottom */
/* vertex. Triangle is than split into two 'baseline' */
/* triangles, and each triangle is filled separately. */
/* One triangle is an upward pointing arrow and the second */
/* Triangle is a downward pointing arrow. */
/* */
/************************************************************************/
triangle(x0,y0, x1,y1, x2,y2, color)
int x0,y0, x1,y1, x2,y2,color;
{
int x[4],y[4],a,b,c,temp;
x[0] = x0;
x[1] = x1;
x[2] = x2;
y[0] = y0;
y[1] = y1;
y[2] = y2;
/* Order the vertices */
a = 0; /* Get lowest left in a */
if (y[0] > y[1]) a = 1;
if (y[a] > y[2]) a = 2;
b = (a+1)%3;
c = (b+1)%3;
if (y[a] == y[b] && x[a] > x[b]) a = b;
if (y[a] == y[c] && x[a] > x[c]) a = c;
b = (a+1)%3;
c = (b+1)%3;
if (y[b] < y[c]) /* Get highest left in b*/
{
temp = b;
b = c;
c = temp;
}
if (y[b] == y[c] && x[b] > x[c])
{
temp = b;
b = c;
c = temp;
}
/* fill 'base' triangles */
if (y[a] == y[b]) /* Fill degenerates */
{
scanline(x[a],x[b],y[b],color);
scanline(x[a],x[c],y[c],color);
}
else if (y[a] == y[c]) /* Fill down arrow */
fill_dw(x[a],y[a],x[b],y[b],x[c],y[c],x[b],y[b],y[c],color);
else if (y[b] == y[c]) /* Fill up arrow */
fill_up(x[a],y[a],x[b],y[b],x[a],y[a],x[c],y[c],y[c],color);
else
{ /* Split into two bases */
y[3] = y[c]; /* one up and one down */
x[3] = x[a] + ((y[3] - y[a])*(long int)(x[b] - x[a]))/(y[b] - y[a]);
if (x[3] < x[c]) /* 'c' is to the right */
{
fill_up(x[a],y[a],x[b],y[b],x[a],y[a],x[c],y[c],y[c],color);
fill_dw(x[a],y[a],x[b],y[b],x[c],y[c],x[b],y[b],y[c],color);
}
else /* 'c' is to the left */
{
fill_up(x[a],y[a],x[c],y[c],x[a],y[a],x[b],y[b],y[c],color);
fill_dw(x[c],y[c],x[b],y[b],x[a],y[a],x[b],y[b],y[c],color);
}
}
}
fill_up(ax,ay, bx,by, cx,cy, dx,dy,y0,color) /* Fill up arrow */
int ax,ay,bx,by,cx,cy,dx,dy,y0,color;
{
int dyab,dycd,y,d1,d2;
dycd = dy - cy;
dyab = by - ay;
for (y = ay; y <= y0; y++)
{
d1 = ((y - ay)*(long)(bx - ax))/dyab;
d2 = ((y - cy)*(long)(dx - cx))/dycd;
scanline(ax+d1, cx+d2, y,color);
}
}
fill_dw(ax,ay, bx,by, cx,cy, dx,dy,y0,color) /* Fill down arrow */
int ax,ay,bx,by,cx,cy,dx,dy,y0,color;
{
int dyab,dycd,y,d1,d2;
dycd = dy - cy;
dyab = by - ay;
for (y = dy; y >= y0; y--)
{
d1 = ((y - ay)*(long)(bx - ax))/dyab;
d2 = ((y - cy)*(long)(dx - cx))/dycd;
scanline(ax+d1, cx+d2, y, color);
}
}