Skip to content

Commit 1b621e1

Browse files
authored
Update digital_differential_analyzer_line_drawing.py
1 parent e8a0ab7 commit 1b621e1

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

graphics/digital_differential_analyzer_line_drawing.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
import matplotlib.pyplot as plt
2+
from typing import List, Tuple
23

4+
def digital_differential_analyzer_line(x1: int, y1: int, x2: int, y2: int) -> Tuple[List[int], List[int]]:
5+
"""
6+
Draw a line using the Digital Differential Analyzer (DDA) algorithm.
7+
8+
Parameters:
9+
x1 (int): The x-coordinate of the starting point.
10+
y1 (int): The y-coordinate of the starting point.
11+
x2 (int): The x-coordinate of the ending point.
12+
y2 (int): The y-coordinate of the ending point.
13+
14+
Returns:
15+
Tuple[List[int], List[int]]: A tuple of two lists, where the first list contains
16+
the x-coordinates of the line points, and the second list contains the y-coordinates.
17+
18+
Example:
19+
>>> digital_differential_analyzer_line(0, 0, 3, 2)
20+
([0, 1, 2, 3], [0, 1, 1, 2])
21+
"""
322

4-
def dda_line(x1, y1, x2, y2):
523
# Calculate the differences in x and y coordinates
624
dx = x2 - x1
725
dy = y2 - y1
@@ -28,20 +46,19 @@ def dda_line(x1, y1, x2, y2):
2846

2947
return x_points, y_points
3048

31-
3249
if __name__ == "__main__":
3350
# Input the coordinates of the two endpoints of the line
34-
x1 = int(input("Enter x1: "))
35-
y1 = int(input("Enter y1: "))
36-
x2 = int(input("Enter x2: "))
37-
y2 = int(input("Enter y2: "))
51+
x1 = int(input("Enter the x-coordinate of the starting point: "))
52+
y1 = int(input("Enter the y-coordinate of the starting point: "))
53+
x2 = int(input("Enter the x-coordinate of the ending point: "))
54+
y2 = int(input("Enter the y-coordinate of the ending point: "))
3855

39-
# Calculate the points using DDA algorithm
40-
x_points, y_points = dda_line(x1, y1, x2, y2)
56+
# Calculate the points using the Digital Differential Analyzer (DDA) algorithm
57+
x_points, y_points = digital_differential_analyzer_line(x1, y1, x2, y2)
4158

4259
# Plot the line using Matplotlib
4360
plt.plot(x_points, y_points, marker="o")
44-
plt.title("DDA Line Drawing Algorithm")
61+
plt.title("Digital Differential Analyzer Line Drawing Algorithm")
4562
plt.xlabel("X-axis")
4663
plt.ylabel("Y-axis")
4764
plt.grid()

0 commit comments

Comments
 (0)