Skip to content

Instantly share code, notes, and snippets.

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