给定xlen
的是delta-x,ylen
是delta-y,len
是行的长度,为什么是这样的代码:
//Bresenham implementation
float x = x0, y = y0;
if (slope < 1) {
while (x < xlen) {
paintpt(x, y));
x += step;
if (left.y > right.y)
y += slope * step;
else
y -= slope * step;
}
}
比这段代码更有效吗?
//Naive vector addition
int x = x0, y = y0;
float xinc = xlen / len, yinc = ylen / len;
for (float i = 0; i < len; i++) {
paintpt(x, y);
x += i * xinc;
y += i * yinc;
}
(我的意思是,除了初始化之外,显然。假设你只得到了线的长度和方向,并且必须退出斜坡等等。)