我已经在 python 中实现了来自 Wikipedia 的Bresenham 算法,但是对于某些行它不起作用,例如从 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