2011-08-23 40 views
1

我已經實現了維基百科中的Bresenham algorithm在Python中,但對於一些行它不起作用,如從1,0到0,1它不停止並繼續做一個超長線Bresenham行不終止

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 

回答

1

你缺少調用absdxdy初始化:

dx = abs(x1 - x0) 
dy = abs(y1 - y0)