0
我正在尝试使用Bresenham的线算法来计算网格上的视野。我正在使用的代码计算行没有问题,但我有问题让它总是返回从起点运行到终点的行。什么我需要做的,使所有线路返回从(X0,Y0)运行到(X1,Y1)修改Bresenham的线算法
def bresenham_line(self, x0, y0, x1, y1):
steep = abs(y1 - y0) > abs(x1 - x0)
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if x0 > x1:
x0, x1 = x1, x0
y0, y1 = y1, y0
if y0 < y1:
ystep = 1
else:
ystep = -1
deltax = x1 - x0
deltay = abs(y1 - y0)
error = -deltax/2
y = y0
line = []
for x in range(x0, x1 + 1):
if steep:
line.append((y,x))
else:
line.append((x,y))
error = error + deltay
if error > 0:
y = y + ystep
error = error - deltax
return line
我会在这里使用的xrange,为您的X0和X1可能是相距甚远相当。 – clahey 2010-04-29 04:18:17