我想在c,wikipedia简化版本中实现Bresenham的行算法。 我的代码被卡住到一个无限循环,我不明白为什么! (虽然我很确定它与我的知识有关)Bresenham的行功能C
void Draw_line (unsigned int x0, unsigned int y0,unsigned int x1, unsigned int y1)
{
unsigned int dx = abs(x1-x0);
unsigned int dy = abs(y1-y0);
signed short sx,sy;
signed int err,e2;
if (x0 < x1) {sx = 1;} else {sx = -1;}
if (y0 < y1) {sy = 1;} else {sy = -1;}
err = dx-dy;
while (!(x0==x1 && y0==y1))
{
GLCD_PutPixel(x0, y0);
e2 = 2*err;
if (e2 > -dy)
{
err = err - dy;
x0 += sx;
}
if (e2 < dx)
{
err = err + dx;
y0 += sy;
}
}
}
谢谢!
编辑:有条件的循环是错误的,所以它没有绘制直线,将其改为正确的。
http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm – user1095108
当您不知道任何其他技术时,调试代码的最佳方式是添加printf语句。 –
如果您始终使用带符号整数,则您的代码应该可以工作。 –