私はPythonでブレゼンハムのアルゴリズムの実装を書きました(ウィキペディアの記事に続いて)、そしてそれは特定の角度の線を除いて正しく動作します。45〜90度、または135〜270度の範囲で延長する必要があるすべての線は、代わりに線y=xに沿って延長されます。
これが私のコードです:
def bresenham(origin, dest):
# debug code
print origin
print dest
# end debug code
x0 = origin[0]; y0 = origin[1]
x1 = dest[0]; y1 = dest[1]
steep = abs(y1 - y0) > abs(x1 - x0)
backward = x0 > x1
if steep:
x0, y0 = y0, x0
x1, y1 = y1, x1
if backward:
x0, x1 = x1, x0
y0, y1 = y1, y0
dx = x1 - x0
dy = abs(y1 - y0)
error = dx / 2
y = y0
if y0 < y1: ystep = 1
else: ystep = -1
result = []
#if x0 > x1: xstep = -1
#else: xstep = 1
# debug code
print "x0 = %d" % (x0)
print "x1 = %d" % (x1)
print "y0 = %d" % (y0)
print "y1 = %d" % (y1)
for x in range(x0, x1):
if steep: result.append((y,x))
else: result.append((x,y))
error -= dy
if error < 0:
y += ystep
error += dx
# ensure the line extends from the starting point to the destination
# and not vice-versa
if backward: result.reverse()
print result
return result
誰かが私が台無しにしているものを見ますか?
編集:
関数に印刷コードを追加しました。
(0,0)はディスプレイの左上にあります。
私のテストフレームワークは非常に単純です。これはスタンドアロン関数なので、2つのポイントを渡すだけです。
origin =(416、384)
dest =(440、347)
bresenham(origin、dest)
( 416、384)(440、347
)
x0 = 384
x1 = 347
y0 = 416
y1 = 440
[]