简化的 Bresenham 线算法:它*到底*做什么?

发布于 2024-12-15 13:17:44 字数 936 浏览 1 评论 0原文

根据维基百科关于 Bresenham 线算法的文章,我已经实现了其中描述的简化版本,我的 Java 实现看起来如下像这样:

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 轴上的步骤之间的比率 - 但现在我应该记录我无法清楚地表达代码在做什么,它的用途是什么,以及为什么 if 语句是这样的,它们是怎样的,以及为什么 err 在方式如代码中所示。

维基百科没有指出任何更详细的解释或来源,所以我想知道:

err到底做了什么以及为什么dxdy 完全按照所示的方式使用布雷森汉姆直线算法的简化版本来保持水平和垂直步长之间的正确比例?

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;
    }
}

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-12-22 13:17:44

直线方程有多种形式,最熟悉的一种是y=m*x+b。现在,如果m=dy/dxc = dx*b,则dx*y = dy*x + c。写成 f(x) = dy*x - dx*y + c,我们有 f(x,y) = 0 iff (x,y)< /code> 是给定线上的一个点。

如果将 x 前进一个单位,f(x,y) 会改变 dy;如果将 y 前进一个单位,f(x,y) 会改变 dx
在您的代码中,err 表示线性函数 f(x,y) 的当前值,语句序列

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

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

表示前进 xy 一个单位(在 sxsy 方向),从而对函数值产生影响。如前所述,对于线上的点,f(x,y) 为零;线一侧的点为正,另一侧的点为负。 if 测试确定前进 x 是否会比前进 y 更接近所需线,反之亦然,或两者兼而有之。

初始化err = dx - dy;旨在最小化偏移误差;如果放大绘图比例,您会发现计算的线可能不会以不同的初始化位于所需的线的中心。

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.

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;

and

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

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.

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.

无言温柔 2024-12-22 13:17:44

只是想在 jwpat 的出色答案中添加一点“为什么”信息。

使用 f(x) = dy*x - dx*y + c 公式的目的是加快计算速度。此公式使用整数算术(更快),而传统的 y = mx + b 公式使用浮点算术(更慢)。

Just want to add one bit of "why" information to jwpat's excellent answer.

The point of using the f(x) = dy*x - dx*y + c formulation is to speed up the calculation. This formulation uses integer arithmetic (faster), whereas the traditional y = mx + b formulation uses floating point arithmetic (slower).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文