RosettaCodeData/Task/Bitmap-Bresenhams-line-algo.../Python/bitmap-bresenhams-line-algo...

17 lines
440 B
Python

from fractions import Fraction
def line(self, x0, y0, x1, y1):
rev = reversed
if abs(y1 - y0) <= abs(x1 - x0):
x0, y0, x1, y1 = y0, x0, y1, x1
rev = lambda x: x
if x1 < x0:
x0, y0, x1, y1 = x1, y1, x0, y0
leny = abs(y1 - y0)
for i in range(leny + 1):
self.set(*rev((round(Fraction(i, leny) * (x1 - x0)) + x0, (1 if y1 > y0 else -1) * i + y0)))
Bitmap.line = line
# see test code above