void line()
{
int x1 = 10, y1 = 10, x2 = 300, y2 = 500 , x, y;
int dx, dy, //deltas
e; // decision parameter
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1 ,0, 0);
setPixel(x1, y1); //plot first point
// difference between starting and ending points
dx = x2 - x1;
dy = y2 - y1;
e = 2 * dy - dx;
x = x1; y = y1;
for(int k = 0; k < dx - 1; ++k)
{
if(e < 0)
{
//next pixel: (x+1, y)
e = e + 2*dy;
}
else
{
//next pixel: (x+1, y+1)
e = e + 2*dy - 2*dx;
++y;
}
++x;
setPixel(x, y);
}
glFlush();
}
e = 2*dy - dx
從何而來?爲什麼我們增加2*dy
或2*dy - 2*dx
?Bresenham線算法 - 決策參數來自哪裏?
我見過該算法具有不同的決策參數,例如最初e = dx/2,然後增加/減少dx或dy。 我可以從我的第一篇文章中獲得的公式從行的y = ax + b函數中獲取嗎?他們究竟做了什麼? – user2252786