Generation

generate functionSun, 13 Nov 2022

bresenhams algorithm

def bresenham(x1, y1, x2, y2): """ Bresenham's line algorithm - returns a list of all pixels in a line https://en.wikipedia.org/wiki/Bresenham's_line_algorithm """ dx = abs(x2 - x1) dy = abs(y2 - y1) steep = dy > dx if steep: x1, y1 = y1, x1 x2, y2 = y2, x2 if x1 > x2: x1, x2 = x2, x1 y1, y2 = y2, y1 dx = abs(x2 - x1) dy = abs(y2 - y1) offset = 0 threshold = dx y = y1 for x in range(x1, x2): if steep: points.append((y, x)) else: points.append((x, y)) offset += dy

Questions about programming?Chat with your personal AI assistant