简化的 Bresenham 线算法:它*到底*做什么?
根据维基百科关于 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
到底做了什么以及为什么dx
和dy
完全按照所示的方式使用布雷森汉姆直线算法的简化版本来保持水平和垂直步长之间的正确比例?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
直线方程有多种形式,最熟悉的一种是
y=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
iff(x,y)< /code> 是给定线上的一个点。
如果将
x
前进一个单位,f(x,y)
会改变dy
;如果将y
前进一个单位,f(x,y)
会改变dx
。在您的代码中,
err
表示线性函数f(x,y)
的当前值,语句序列和
表示前进
x
或y
一个单位(在sx
或sy
方向),从而对函数值产生影响。如前所述,对于线上的点,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 ifm=dy/dx
andc = dx*b
, thendx*y = dy*x + c
. Writingf(x) = dy*x - dx*y + c
, we havef(x,y) = 0
iff(x,y)
is a point on given line.If you advance
x
one unit,f(x,y)
changes bydy
; if you advancey
one unit,f(x,y)
changes bydx
.In your code,
err
represents the current value of the linear functionalf(x,y)
, and the statement sequencesand
represent advancing
x
ory
one unit (insx
orsy
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. Theif
tests determine whether advancingx
will stay closer to the desired line than advancingy
, 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.只是想在 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 traditionaly = mx + b
formulation uses floating point arithmetic (slower).