moai/src/moaicore/MOAIImage.cpp
//----------------------------------------------------------------//
/**
 * Draws a line between two points p1(p1x,p1y) and p2(p2x,p2y).
 * This function is based on the Bresenham's line algorithm and is highly
 * optimized to be able to draw lines very quickly. There is no floating point
 * arithmetic nor multiplications and divisions involved. Only addition,
 * subtraction and bit shifting are used.
 *
 * Note that you have to define your own customized setPixel(x,y) function,
 * which essentially lights a pixel on the screen.
 */


Note that you have to define your own customized setPixel(x,y) function,
which essentially lights a pixel on the screen.
Note that you have to define your own customized setPixel ( x y ) function which essentially lights a pixel on the screen .
void MOAIImage::DrawLine(int p1x, int p1y, int p2x, int p2y, u32 color)
{
    int F, x, y;
    if (p1x > p2x)  
    {
        swap(p1x, p2x);
        swap(p1y, p2y);
    }
    if (p1x == p2x)
    {
        if (p1y > p2y)  
        {
            swap(p1y, p2y);
        }
        x = p1x;
        y = p1y;
        while (y <= p2y)
        {
            this->SetColor(x, y, color);
            y++;
        }
        return;
    }
    else if (p1y == p2y)
    {
        x = p1x;
        y = p1y;
        while (x <= p2x)
        {
            this->SetColor(x, y, color);
            x++;
        }
        return;
    }
    int dy            = p2y - p1y;  
    int dx            = p2x - p1x;  
    int dy2           = (dy << 1);  
    int dx2           = (dx << 1);
    int dy2_minus_dx2 = dy2 - dx2;  
    int dy2_plus_dx2  = dy2 + dx2;
    if (dy >= 0)    
    {
        if (dy <= dx)
        {
            F = dy2 - dx;    
            x = p1x;
            y = p1y;
            while (x <= p2x)
            {
                this->SetColor(x, y, color);
                if (F <= 0)
                {
                    F += dy2;
                }
                else
                {
                    y++;
                    F += dy2_minus_dx2;
                }
                x++;
            }
        }
        else
        {
            F = dx2 - dy;    
            y = p1y;
            x = p1x;
            while (y <= p2y)
            {
                this->SetColor(x, y, color);
                if (F <= 0)
                {
                    F += dx2;
                }
                else
                {
                    x++;
                    F -= dy2_minus_dx2;
                }
                y++;
            }
        }
    }
    else    
    {
        if (dx >= -dy)
        {
            F = -dy2 - dx;    
            x = p1x;
            y = p1y;
            while (x <= p2x)
            {
                this->SetColor(x, y, color);
                if (F <= 0)
                {
                    F -= dy2;
                }
                else
                {
                    y--;
                    F -= dy2_plus_dx2;
                }
                x++;
            }
        }
        else
        {
            F = dx2 + dy;    
            y = p1y;
            x = p1x;
            while (y >= p2y)
            {
                this->SetColor(x, y, color);
                if (F <= 0)
                {
                    F += dx2;
                }
                else
                {
                    x++;
                    F += dy2_plus_dx2;
                }
                y--;
            }
        }
    }
}
void MOAIImage :: DrawLine ( int p1x , int p1y , int p2x , int p2y , u32 color ) { int F , x , y ; if ( p1x > p2x ) { swap ( p1x , p2x ) ; swap ( p1y , p2y ) ; } if ( p1x == p2x ) { if ( p1y > p2y ) { swap ( p1y , p2y ) ; } x = p1x ; y = p1y ; while ( y <= p2y ) { this -> SetColor ( x , y , color ) ; y ++ ; } return ; } else if ( p1y == p2y ) { x = p1x ; y = p1y ; while ( x <= p2x ) { this -> SetColor ( x , y , color ) ; x ++ ; } return ; } int dy = p2y - p1y ; int dx = p2x - p1x ; int dy2 = ( dy << 1 ) ; int dx2 = ( dx << 1 ) ; int dy2_minus_dx2 = dy2 - dx2 ; int dy2_plus_dx2 = dy2 + dx2 ; if ( dy >= 0 ) { if ( dy <= dx ) { F = dy2 - dx ; x = p1x ; y = p1y ; while ( x <= p2x ) { this -> SetColor ( x , y , color ) ; if ( F <= 0 ) { F += dy2 ; } else { y ++ ; F += dy2_minus_dx2 ; } x ++ ; } } else { F = dx2 - dy ; y = p1y ; x = p1x ; while ( y <= p2y ) { this -> SetColor ( x , y , color ) ; if ( F <= 0 ) { F += dx2 ; } else { x ++ ; F -= dy2_minus_dx2 ; } y ++ ; } } } else { if ( dx >= - dy ) { F = - dy2 - dx ; x = p1x ; y = p1y ; while ( x <= p2x ) { this -> SetColor ( x , y , color ) ; if ( F <= 0 ) { F -= dy2 ; } else { y -- ; F -= dy2_plus_dx2 ; } x ++ ; } } else { F = dx2 + dy ; y = p1y ; x = p1x ; while ( y >= p2y ) { this -> SetColor ( x , y , color ) ; if ( F <= 0 ) { F += dx2 ; } else { x ++ ; F += dy2_plus_dx2 ; } y -- ; } } } }
Draws a line between two points p1(p1x,p1y) and p2(p2x,p2y).
Draws a line between two points p1 ( p1x p1y ) and p2 ( p2x p2y ) .
// Swap points if p1 is on the right of p2 // Handle trivial cases separately for algorithm speed up. // Trivial case 1: m = +/-INF (Vertical line) // Swap y-coordinates if p1 is above p2 // Trivial case 2: m = 0 (Horizontal line) // y-increment from p1 to p2 // x-increment from p1 to p2 // dy << 1 == 2*dy // precompute constant for speed up // m >= 0 // Case 1: 0 <= m <= 1 (Original case) // initial F // Case 2: 1 < m < INF (Mirror about y=x line // replace all dy by dx and dx by dy) // initial F // m < 0 // Case 3: -1 <= m < 0 (Mirror about x-axis, replace all dy by -dy) // initial F // Case 4: -INF < m < -1 (Mirror about x-axis and mirror // about y=x line, replace all dx by -dy and dy by dx) // initial F