src/drawables.cpp
// Fill a triangle - slope method
// Original Author: Adafruit Industries
Fill a triangle - slope method
Original Author: Adafruit Industries
Fill a triangle - slope method Original Author : Adafruit Industries
void filltriangle(int x0, int y0, int x1, int y1, int x2, int y2) {
    int a, b, y, last;
    if (y0 > y1) {
        std::swap(y0, y1);
        std::swap(x0, x1);
    }
    if (y1 > y2) {
        std::swap(y2, y1);
        std::swap(x2, x1);
    }
    if (y0 > y1) {
        std::swap(y0, y1);
        std::swap(x0, x1);
    }
    if (y0 == y2) { 
        a = b = x0;
        if (x1 < a)
            a = x1;
        else if (x1 > b)
            b = x1;
        if (x2 < a)
            a = x2;
        else if (x2 > b)
            b = x2;
        fline(a, b, y0);
        return;
    }
    int dx01 = x1 - x0, dy01 = y1 - y0, dx02 = x2 - x0, dy02 = y2 - y0, dx12 = x2 - x1,
        dy12 = y2 - y1;
    int sa = 0, sb = 0;
    if (y1 == y2)
        last = y1; 
    else
        last = y1 - 1; 
    for (y = y0; y <= last; y++) {
        a = x0 + sa / dy01;
        b = x0 + sb / dy02;
        sa += dx01;
        sb += dx02;
        fline(a, b, y);
    }
    sa = dx12 * (y - y1);
    sb = dx02 * (y - y0);
    for (; y <= y2; y++) {
        a = x1 + sa / dy12;
        b = x0 + sb / dy02;
        sa += dx12;
        sb += dx02;
        fline(a, b, y);
    }
}
void filltriangle ( int x0 , int y0 , int x1 , int y1 , int x2 , int y2 ) { int a , b , y , last ; if ( y0 > y1 ) { std :: swap ( y0 , y1 ) ; std :: swap ( x0 , x1 ) ; } if ( y1 > y2 ) { std :: swap ( y2 , y1 ) ; std :: swap ( x2 , x1 ) ; } if ( y0 > y1 ) { std :: swap ( y0 , y1 ) ; std :: swap ( x0 , x1 ) ; } if ( y0 == y2 ) { a = b = x0 ; if ( x1 < a ) a = x1 ; else if ( x1 > b ) b = x1 ; if ( x2 < a ) a = x2 ; else if ( x2 > b ) b = x2 ; fline ( a , b , y0 ) ; return ; } int dx01 = x1 - x0 , dy01 = y1 - y0 , dx02 = x2 - x0 , dy02 = y2 - y0 , dx12 = x2 - x1 , dy12 = y2 - y1 ; int sa = 0 , sb = 0 ; if ( y1 == y2 ) last = y1 ; else last = y1 - 1 ; for ( y = y0 ; y <= last ; y ++ ) { a = x0 + sa / dy01 ; b = x0 + sb / dy02 ; sa += dx01 ; sb += dx02 ; fline ( a , b , y ) ; } sa = dx12 * ( y - y1 ) ; sb = dx02 * ( y - y0 ) ; for ( ; y <= y2 ; y ++ ) { a = x1 + sa / dy12 ; b = x0 + sb / dy02 ; sa += dx12 ; sb += dx02 ; fline ( a , b , y ) ; } }
Fill a triangle - slope method
Original Author: Adafruit Industries
Fill a triangle - slope method Original Author : Adafruit Industries
// Sort coordinates by Y order (y2 >= y1 >= y0) // All on same line case // For upper part of triangle, find scanline crossings for segment // 0-1 and 0-2.  If y1=y2 (flat-bottomed triangle), the scanline y // is included here (and second loop will be skipped, avoiding a / // error there), otherwise scanline y1 is skipped here and handle // in the second loop...which also avoids a /0 error here if y0=y // (flat-topped triangle) // Include y1 scanline // Skip it // longhand a = x0 + (x1 - x0) * (y - y0) / (y1 - y0) //          b = x0 + (x2 - x0) * (y - y0) / (y2 - y0) // For lower part of triangle, find scanline crossings for segment // 0-2 and 1-2.  This loop is skipped if y1=y2 // longhand a = x1 + (x2 - x1) * (y - y1) / (y2 - y1) //          b = x0 + (x2 - x0) * (y - y0) / (y2 - y0)