Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create digital differential analyzer_line.py #10929

Merged
merged 36 commits into from
Dec 31, 2024
Merged
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c62f30a
Create DDA_line_drawing.py
nababuddin Oct 25, 2023
d08be9a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2023
220559a
Rename DDA_line_drawing.py to digital differential analyzer_line_draw…
nababuddin Oct 26, 2023
5d4b127
Rename DDA_line_drawing.py to digital_differential_analyzer_line_draw…
nababuddin Oct 26, 2023
e8a0ab7
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 26, 2023
1b621e1
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
8391c5f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
70764bb
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
d84e4ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
a6b452f
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
06f87eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
7f2ee3c
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
f415328
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
5cae215
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
5234d02
Update digital_differential_analyzer_line_drawing.py
nababuddin Oct 27, 2023
f34960f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
842ad9e
Apply suggestions from code review
nababuddin Oct 28, 2023
74b5dd1
Update and rename digital_differential_analyzer_line_drawing.py to di…
nababuddin Oct 28, 2023
dc1f4b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
836ce48
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
3aa73ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
d9f2d33
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
a4dcebf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
6d1844a
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
67ca0e6
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
94972ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
9287725
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
b4b89ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
6f0540d
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
11e9315
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
93ed88b
Update digital_differential_analyzer_line.py
nababuddin Oct 28, 2023
ac7607e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
7ce5261
Apply suggestions from code review
tianyizheng02 Dec 31, 2024
8dab7f4
Fix doctest
tianyizheng02 Dec 31, 2024
4a91773
Trigger GH workflows
tianyizheng02 Dec 31, 2024
6f381df
Fix function call in main block
tianyizheng02 Dec 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions graphics/digital_differential_analyzer_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import matplotlib.pyplot as plt


def digital_differential_analyzer_line(
nababuddin marked this conversation as resolved.
Show resolved Hide resolved
x1: int, y1: int, x2: int, y2: int
) -> list[tuple[int, int]]:
"""
Draws a line between two points using the DDA algorithm.

Args:
- x1, y1: Coordinates of the starting point.
- x2, y2: Coordinates of the ending point.

Returns:
- List of coordinate points that form the line.

>>> digital_differential_analyzer_line(1, 1, 4, 4)
[(2, 2), (3, 3), (4, 4)]
"""

dx = x2 - x1
dy = y2 - y1
steps = max(abs(dx), abs(dy))
x_increment = dx / float(steps)
y_increment = dy / float(steps)
coordinates = []
x: float = x1
y: float = y1
for _ in range(steps):
x += x_increment
y += y_increment
coordinates.append((int(round(x)), int(round(y))))
return coordinates


if __name__ == "__main__":
import doctest

doctest.testmod()

x1 = int(input("Enter the x-coordinate of the starting point: "))
y1 = int(input("Enter the y-coordinate of the starting point: "))
x2 = int(input("Enter the x-coordinate of the ending point: "))
y2 = int(input("Enter the y-coordinate of the ending point: "))
coordinates = digital_differential_analyzer_line(x1, y1, x2, y2)
x_points, y_points = zip(*coordinates)
plt.plot(x_points, y_points, marker="o")
plt.title("Digital Differential Analyzer Line Drawing Algorithm")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid()
plt.show()