范一/
int Draw_Line(int x1, int y1, // 起始点
int x2, int y2, // 终点
unsigned char color, // 颜色像素
unsigned char *vb_start, int lpitch) // video buffer and memory pitch
{
// this function draws a line from xo,yo to x1,y1 using differential error
// terms (based on Bresenahams work)
int dx, // difference in x's
dy, // difference in y's
dx2, // dx,dy * 2
dy2,
x_inc, // amount in pixel space to move during drawing
y_inc, // amount in pixel space to move during drawing
error, // the discriminant i.e. error i.e. decision variable
index; // used for looping
// pre-compute first pixel address in video buffer
vb_start = vb_start + x1 + y1 * lpitch;
// compute horizontal and vertical deltas
dx = x2-x1;
dy = y2-y1;
// test which direction the line is going in i.e. slope angle
if (dx>=0)
{
x_inc = 1;
} // end if line is moving right
else
{
x_inc = -1;
dx = -dx; // need absolute value
} // end else moving left
// test y component of slope
if (dy>=0)
{
y_inc = lpitch;
} // end if line is moving down
else
{
y_inc = -lpitch;
dy = -dy; // need absolute value
} // end else moving up
// compute (dx,dy) * 2
dx2 = dx << 1;
dy2 = dy << 1;
// now based on which delta is greater we can draw the line
if (dx > dy) //斜率小于1的情况
{
// initialize error term
error = dy2 - dx;
// draw the line
for (index=0; index <= dx; index++)
{
// set the pixel
*vb_start = color;
// test if error has overflowed
if (error >= 0)
{
error -= dx2;
// move to next line
vb_start += y_inc;
}
// adjust the error term
error += dy2
// move to the next pixel
vb_start += x_inc;
}
}
else //斜率大于等于1的情况
{
// initialize error term
error = dx2 - dy;
for (index=0; index <= dy; index++)
{
<