bresenhams line algorithm
unknown
python
2 years ago
610 B
19
Indexable
def draw_line(x0, y0, x1, y1):
    dx = abs(x1 - x0)
    dy = abs(y1 - y0)
    slope = dy > dx
    if slope:
        x0, y0 = y0, x0
        x1, y1 = y1, x1
    if x0 > x1:
        x0, x1 = x1, x0
        y0, y1 = y1, y0
    dx = abs(x1 - x0)
    dy = abs(y1 - y0)
    error = 0
    delta_error = dy / dx
    y = y0
    y_step = 1 if y0 < y1 else -1
    for x in range(x0, x1 + 1):
        if slope:
            plot_pixel(y, x)
        else:
            plot_pixel(x, y)
        error += delta_error
        if error >= 0.5:
            y += y_step
            error -= 1
Editor is loading...
Leave a Comment