简体布氏线算法:这是什么*究竟*吗?

问题描述:

我Java实现看起来是这样的:

Based on Wikipedia's article on Bresenham's line algorithm I've implemented the simplified version described there, my Java implementation looks like this:

int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);

int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;

int err = dx - dy;

while (true) {
    framebuffer.setPixel(x1, y1, Vec3.one);

    if (x1 == x2 && y1 == y2) {
        break;
    }

    int e2 = 2 * err;

    if (e2 > -dy) {
        err = err - dy;
        x1 = x1 + sx;
    }

    if (e2 < dx) {
        err = err + dx;
        y1 = y1 + sy;
    }
}

现在我明白了 ERR 控制在x轴的步骤相比,在y轴上的步骤之间的比值 - 但现在,我应该文档我不能什么code是做明确前preSS,它是什么,以及为什么的完全的,就算陈述,他们是如何,以及为什么 ERR 作为在code可见的方式发生变化。

Now I do understand that err controls the ratio between steps on the x-axis compared to steps on the y-axis - but now that I'm supposed to document what the code is doing I fail to clearly express, what it is for, and why exactly the if-statements are, how they are, and why err is changed in the way as seen in the code.

*不指向任何更多的相关详细的解释或来源,所以我想知道:

Wikipedia doesn't point to any more detailled explanations or sources, so I'm wondering:

什么precisely确实 ERR 做的,为什么 DX DY 使用完全相同的显示方式,以保持使用布氏线算法的这种简化版本的水平和垂直步长之间的​​正确比例是多少?

What precisely does err do and why are dx and dy used in exactly the shown way to maintain the correct ratio between horizontal and vertical steps using this simplified version of Bresenham's line algorithm?

有一个行各种形式的方程组,最熟悉的存在一个ÿ= M * X + B 。现在,如果 M = DY / DX C = DX * B ,然后 DX * Y = DY * X + C 。写 F(X)= DY * X - DX * Y + C ,我们 F(X,Y)= 0 当且仅当(X,Y)是给线上的点。

There are various forms of equations for a line, one of the most familiar being y=m*x+b. Now if m=dy/dx and c = dx*b, then dx*y = dy*x + c. Writing f(x) = dy*x - dx*y + c, we have f(x,y) = 0 iff (x,y) is a point on given line.

如果你提前 X 一个单元, F(X,Y)变化DY ;如果你预先一个单元, F(X,Y)更改为 DX 。 在您的code, ERR 重presents的当前值的线性函数 F(X,Y),和语句序列

If you advance x one unit, f(x,y) changes by dy; if you advance y one unit, f(x,y) changes by dx. In your code, err represents the current value of the linear functional f(x,y), and the statement sequences

    err = err - dy;
    x1 = x1 + sx;

    err = err + dx;
    y1 = y1 + sy;

再present推进 X 一个单元(在 SX SY 方向),与函数值随之而来的效果。正如前面提到的, F(X,Y)是上线积分为零;它是正面为上的点线的一侧,而负的那些为另一方。该如果测试确定是否推进 X 将保持接近所需的行比推进,反之亦然,或两者兼而有之。

represent advancing x or y one unit (in sx or sy direction), with consequent effect on the function value. As noted before, f(x,y) is zero for points on the line; it is positive for points on one side of the line, and negative for those on the other. The if tests determine whether advancing x will stay closer to the desired line than advancing y, or vice versa, or both.

初​​始化 ERR = DX - 颐; 是为了尽量减少偏移误差;如果你吹你的绘图的规模,你会看到你的计算行可能无法在不同的初始化所需的行居中。

The initialization err = dx - dy; is designed to minimize offset error; if you blow up your plotting scale, you'll see that your computed line may not be centered on the desired line with different initializations.