Logo Search packages:      
Sourcecode: libcaca version File versions  Download package

void caca_fill_triangle ( int  x1,
int  y1,
int  x2,
int  y2,
int  x3,
int  y3,
char  c 
)

Fill a triangle on the screen using the given character.

Parameters:
x1 X coordinate of the first point.
y1 Y coordinate of the first point.
x2 X coordinate of the second point.
y2 Y coordinate of the second point.
x3 X coordinate of the third point.
y3 Y coordinate of the third point.
c Character to fill the triangle with.
Returns:
void

Definition at line 86 of file triangle.c.

References caca_fill_triangle(), and caca_putchar().

Referenced by caca_fill_triangle().

{
    int x, y, xa, xb, xmax, ymax;

    /* Bubble-sort y1 <= y2 <= y3 */
    if(y1 > y2)
    {
        caca_fill_triangle(x2, y2, x1, y1, x3, y3, c);
        return;
    }

    if(y2 > y3)
    {
        caca_fill_triangle(x1, y1, x3, y3, x2, y2, c);
        return;
    }

    /* Promote precision */
    x1 *= 4;
    x2 *= 4;
    x3 *= 4;

    xmax = _caca_width - 1;
    ymax = _caca_height - 1;

    /* Rasterize our triangle */
    for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)
    {
        if(y <= y2)
        {
            xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1);
            xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1);
        }
        else
        {
            xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3);
            xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3);
        }

        if(xb < xa)
        {
            int tmp = xb;
            xb = xa; xa = tmp;
        }

        /* Rescale xa and xb, slightly cropping */
        xa = (xa + 2) / 4;
        xb = (xb - 2) / 4;

        if(xb < 0) continue;
        if(xa > xmax) continue;
        if(xa < 0) xa = 0;
        if(xb > xmax) xb = xmax;

        for(x = xa; x <= xb; x++)
            caca_putchar(x, y, c);
    }
}


Generated by  Doxygen 1.6.0   Back to index