import time t0 = time.time() def log(*msg): print("{} - ".format(str(time.time() - t0)[:5]), *msg) def line(start, target, strict=True): """ adapted from https://github.com/fragkakis/bresenham/blob/master/src/main/java/org/fragkakis/Bresenham.java if strict is true, None is return if an obstacle interrupted the line; else a partial line is returned (from start to obstacle) """ line = [] x0, y0 = start x1, y1 = target reversed = y0 > y1 if reversed: # on fait toujours de bas en haut, du coup on inverse au besoin x0, y0, x1, y1 = x1, y1, x0, y0 # NB : not reversed = forward ; reversed = backward dx = abs(x1 - x0) dy = abs(y1 - y0) sx = 1 if x0 < x1 else -1 sy = 1 if y0 < y1 else -1 err = dx - dy x, y = x0, y0 while 1: e2 = 2 * err if e2 > -1 * dy: err -= dy x += sx if e2 < dx: err += dx y += sy if x == x1 and y == y1: break line.append((x, y)) if reversed: line = line[::-1] line = [start] + line + [target] return line log(line((4, 4), (7, 6))) log(line((7, 6), (4, 4))) # log(line((0, 2), (4, 3))) # test droite / bas # log(line((0, 5), (4, 3))) # test droite / haut # log(line((8, 1), (4, 3))) # test gauche / bas # log(line((8, 5), (4, 3))) # test gauche / haut