// Generalized Integer Bresenham's Algorithm for all quadrants // all variables are assumed integer // sign function returns -1, 0, 1 as its argument is < 0, = 0 or > 0 bresenham_algorithm(x1, y1, x2, y2) // initialize variables x = x1 y = y1 delta_x = abs(x2 - x1) delta_y = abs(y2 - y1) s1 = sign(x2 - x1) s2 = sign(y2 - y1) // interchange delta_x and delta_y, depending on the slope of the line if delta_y > delta_x then interchange(delta_x, delta_y) interchanged is true else interchanged is false end if // initialize the error term to compensate for a nonzero intercept error = 2 * delta_y - delta_x for i = 0 to delta_x set_pixel(x, y) while error > 0 if interchanged is true then x = x + s1 else y = y + s2 end if error = error - 2 * delta_x end while if interchanged is true then y = y + s2 else x = x + s1 end if error = error + 2 * delta_y end for end