/* Draw a line in the image */
void drawColorLine( colorImage image, int row0, int col0,
int row1, int col1, pixel pix)
{
int r, c;
/* For lines less than 45deg with x axis */
/* move from column to column and plot the row on the line */
if ( abs(col1-col0) > abs(row1-row0) )
{
/* make col0 left of col1 */
if ( col0>col1 )
{
int temp = col0; col0 = col1; col1 = temp;
temp = row0; row0 = row1; row1 = temp;
}
for ( c=col0; c<col1; c++ )
{
r = row0 + (c-col0)*(row1-row0)/(col1-col0) ;
setColorPixel( image, r, c, pix );
}
}
/* For lines greater or equal to 45deg with x axis */
/* move from row to row and plot the col on the line */
else
{
/* make row0 above row1 */
if ( row0>row1 )
{
int temp = col0; col0 = col1; col1 = temp;
temp = row0; row0 = row1; row1 = temp;
}
for ( r=row0; r<row1; r++ )
{
c = col0 + (r-row0)*(col1-col0)/(row1-row0) ;
setColorPixel( image, r, c, pix );
}
}
}
Comments: For speed (important in graphics operations), move the calculation of the slope outside of the loop. An optimizing compiler might do this automatically.