Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save remram44/e1009c61b572bd068f7f to your computer and use it in GitHub Desktop.
Save remram44/e1009c61b572bd068f7f to your computer and use it in GitHub Desktop.
def draw_line(pixels, origin, destination):
x, y = x1, y1 = origin
x2, y2 = destination
Δx = abs(x2 - x1)
Δy = abs(y2 - y1)
s1 = sign(x2-x1)
s2 = sign(y2-y1)
inc_inner, inc_outer = ((0, s2), (s1, 0))
if Δy > Δx :
Δx, Δy = Δy, Δx
inc_inner, inc_outer = inc_outer, inc_inner
e = 2 * Δy - Δx
for i in range(1, Δx):
pixels[x,y] = (0, 0, 0)
while e >= 0:
x, y = x + inc_inner[0], y + inc_inner[1]
e -= 2*Δx
x, y = x + inc_outer[0], y + inc_outer[1]
e += 2 * Δy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment