import numpy as np
import matplotlib.pyplot as plt
### Data input
x_init = int(input("Enter starting x-coordinates: "))
y_init = int(input("Enter starting y-coordinates: "))
x_final = int(input("Enter ending x-coordinates: "))
y_final = int(input("Enter ending y-coordinates: "))
#### Digital Differential Analyzer (DDA) Line Drawing Algorithm ####
### Initializations
x_coordinates = []
y_coordinates = []
### Step 01
delta_x = x_final - x_init
delta_y = y_final - y_init
m = delta_y / delta_x
### Step 02
steps = np.max([delta_x, delta_y])
### Step 03
x = x_init
y = y_init
if m < 1:
for i in range(steps):
x_coordinates.append(np.ceil(x))
y_coordinates.append(np.ceil(y))
x += 1
y += m
elif m == 1:
for i in range(steps):
x_coordinates.append(np.ceil(x))
y_coordinates.append(np.ceil(y))
x += 1
y += 1
else:
for i in range(steps):
x_coordinates.append(np.ceil(x))
y_coordinates.append(np.ceil(y))
x += 1/m
y += 1
# append the final point
x_coordinates.append(np.ceil(x_final))
y_coordinates.append(np.ceil(y_final))
# transform coordinates into a numpy array and print them out
x_coordinates = np.array(x_coordinates, dtype = int)
y_coordinates = np.array(y_coordinates, dtype = int)
print(np.vstack((x_coordinates, y_coordinates)).T)
### Step 04
plt.plot(x_coordinates, y_coordinates)
plt.title("DDA Line Drawing Algorithm")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()