Industrial Training




Bresenham's Line Drawing Algorithm page-2



bressline ( int x1, int y1, int x2, int y2 )

{

int incdec, t, i ;

if ( x1 > x2 )

{

t = x1 ; x1 = x2 ; x2 = t ;

t = y1 ; y1 = y2 ; y2 = t ;

}

dx = x2 - x1 ; dy = y2 - y1 ;

if ( dx == 0 ) /* vertical line */

{

if ( y1 > y2 )

{

t = y1 ; y1 = y2 ; y2 = t ;

}

for ( i = y1 ; i <= y2 ; i++ )

putpixel ( x1, i, WHITE ) ;


return ;

}

if ( dy == 0 ) /* horizontal line */

{

for ( i = x1 ; i < x2 ; i++ )

putpixel ( i, y1, WHITE ) ;


return ;

}

/* 0 < m < 1 */

if ( dy < dx && dy > 0 )

{

e_noinc = 2 * dy ;

e = 2 * dy - dx ;

e_inc = 2 * ( dy - dx ) ;

drawline ( x1, y1, x2, y2, PREDX, INCR ) ;

}

/* m = 1 */

if ( dy == dx && dy > 0 )

{

e_noinc = 2 * dy ;

e = 2 * dy - dx ;

e_inc = 2 * ( dy - dx ) ;

drawline ( x1, y1, x2, y2, PREDX, INCR ) ;

}


/* 1 < m < infinity */

if ( dy > dx && dy > 0 )

{

e_noinc = 2 * dx ;

e = 2 * dx - dy ;

e_inc = 2 * ( dx - dy ) ;

drawline ( x1, y1, x2, y2, PREDY, INCR ) ;

}

/* 0 > m > -1 */

if ( -dy < dx && dy < 0 )

{

dy = -dy ;

e_noinc = 2 * dy ;

e = 2 * dy - dx ;

e_inc = 2 * ( dy - dx ) ;

drawline ( x1, y1, x2, y2, PREDX, DECR ) ;

}


/* m = -1 */

if ( dy == -dx && dy < 0 )

{

dy = -dy ;

e_noinc = ( 2 * dy ) ;

e = 2 * dy - dx ;

e_inc = 2 * ( dy - dx ) ;

drawline ( x1, y1, x2, y2, PREDX, DECR ) ;

}

/* -1 > m > 0 */

if ( -dy > dx && dy < 0 )

{

dx = -dx ;

e_noinc = - ( 2*dx ) ; e = 2 * dx - dy ;

e_inc = - 2 * ( dx - dy ) ;

drawline ( x2, y2, x1, y1, PREDY, DECR ) ;

}

}


drawline ( int x1, int y1, int x2, int y2, int pred, int incdec )

{

int i, start, end, var ;

if ( pred == PREDX )

{

start = x1 ; end = x2 ; var = y1 ;

}

else

{

start = y1 ; end = y2 ; var = x1 ;

}


for ( i = start ; i <= end ; i++ )

{

if ( pred == PREDY )

putpixel ( var, i, WHITE ) ;

else

putpixel ( i, var, WHITE ) ;


if ( e < 0 )

e += e_noinc ;

else

{

var += incdec ;

e += e_inc ;

}

}

}



Hi I am Pluto.