Skip to content

Commit 6f0540d

Browse files
authored
Update digital_differential_analyzer_line.py
1 parent b4b89ca commit 6f0540d

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

graphics/digital_differential_analyzer_line.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
import matplotlib.pyplot as plt
22

3-
43
def digital_differential_analyzer_line(
54
x1: int, y1: int, x2: int, y2: int
65
) -> list[tuple[int, int]]:
6+
"""
7+
Draws a line between two points using the Digital Differential Analyzer (DDA) algorithm.
8+
9+
Args:
10+
- x1, y1: Coordinates of the starting point.
11+
- x2, y2: Coordinates of the ending point.
12+
13+
Returns:
14+
- List of coordinate points that form the line.
15+
16+
>>> digital_differential_analyzer_line(1, 1, 4, 4)
17+
[(2, 2), (3, 3), (4, 4)]
18+
"""
19+
720
dx = x2 - x1
821
dy = y2 - y1
922
steps = max(abs(dx), abs(dy))
1023
x_increment = dx / float(steps)
1124
y_increment = dy / float(steps)
1225
coordinates = []
13-
x = x1
14-
y = y1
26+
x: float = x1
27+
y: float = y1
1528
for _ in range(steps):
1629
x += x_increment
1730
y += y_increment
1831
coordinates.append((int(round(x)), int(round(y))))
1932
return coordinates
2033

21-
2234
if __name__ == "__main__":
35+
import doctest
36+
doctest.testmod()
37+
2338
x1 = int(input("Enter the x-coordinate of the starting point: "))
2439
y1 = int(input("Enter the y-coordinate of the starting point: "))
2540
x2 = int(input("Enter the x-coordinate of the ending point: "))

0 commit comments

Comments
 (0)