我已经用python实现了维基百科中的Bresenham algorithm,但对于某些行,它不能工作,比如从1,0到0,1它不会停止,并继续生成超长的行
def line(x0, y0, x1, y1):
dx = x1 - x0
dy = y1 - y0
sx = x0 < x1 and 1 or -1
sy = y0 < y1 and 1 or -1
err = dx - dy
points = []
x, y = x0, y0
while True:
points += [(x, y)]
if x == x1 and y == y1:
break
e2 = err * 2
if e2 > -dy:
err -= dy
x += sx
if e2 < dx:
err += dx
y += sy
return points发布于 2011-08-23 18:08:03
在初始化dx和dy时,您缺少对abs的调用
dx = abs(x1 - x0)
dy = abs(y1 - y0)发布于 2011-08-23 18:05:32
您在"if e2 > -dy:“中有一个类型。减号是错误的。
https://stackoverflow.com/questions/7159228
复制相似问题