import numpy as np
import matplotlib.pyplot as plt
img=np.ones((600,600,3))
def DrawLine(x1,y1,x2,y2):
dx = abs(x2-x1)
dy = abs(y2-y1)
x=x1
y=y1
if x1<x2:
xs=1
else:
xs=-1
if y1<y2:
ys=1
else:
ys=-1
if dx>=dy:
p=2*dy-dx
while x!=x2:
x=x+xs
if p > 0:
y=y+ys
p=p+2*dx-2*dy
else:
p=p+2*dy
img[y,x]= 0
else: dy>=dx
p=2*dx-dy
while y!=y2:
y=y+ys
if p > 0:
x=x+xs
p=p+2*dy-2*dx
else:
p=p+2*dx
img[y,x]= 0
return;
%matplotlib qt
DrawLine(100,300,150,300)
DrawLine(100,400,150,400)
DrawLine(100,300,100,400)
DrawLine(150,300,150,400)
DrawLine(200,300,250,300)
DrawLine(250,300,250,350)
DrawLine(250,350,200,400)
DrawLine(200,400,250,400)
DrawLine(300,300,350,300)
DrawLine(350,300,300,350)
DrawLine(300,350,350,350)
DrawLine(350,350,300,400)
plt.imshow(img)
plt.show()
else: dy>=dx
p=2*dx-dy
while y!=y2:
y=y+ys
if p > 0:
x=x+xs
p=p+2*dy-2*dx
else:
p=p+2*dx
img[y,x]= 0
return;
%matplotlib qt
DrawLine(100,300,150,300)
DrawLine(100,400,150,400)
DrawLine(100,300,100,400)
DrawLine(150,300,150,400)
DrawLine(200,300,250,300)
DrawLine(250,300,250,350)
DrawLine(250,350,200,400)
DrawLine(200,400,250,400)
DrawLine(300,300,350,300)
DrawLine(350,300,300,350)
DrawLine(300,350,350,350)
DrawLine(350,350,300,400)
plt.imshow(img)
plt.show()