直线扫描转换常用算法——Bresenham画线算法
算法2.1 画斜率0≤k≤1的直线的Bresenham画线算法的C语言程序:
void BresenhamLine (int x0, int y0, int x1, int y1, long color)
{
int x, y, dx, dy;
float k, e;
dx = x1-x0;
dy = y1- y0;
e = -0.5;
x = x0;
y = y0;
if (dx = = 0)
{
for (i=0;i≤dy;i++)
{
DrawPixel (x, y+i, color);//画像素(x,y+i)
}
return;
}
k = dy/dx;
for (i=0;i≤dx;i++)
{
DrawPixel (x, y, color);//画像素(x,y)
x++;
e += k;
if (e≥0)
{
y++;
e--;
}
}
}