-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Create digital differential analyzer_line.py #10929
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
Changes from 5 commits
c62f30a
d08be9a
220559a
5d4b127
e8a0ab7
1b621e1
8391c5f
70764bb
d84e4ec
a6b452f
06f87eb
7f2ee3c
f415328
5cae215
5234d02
f34960f
842ad9e
74b5dd1
dc1f4b2
836ce48
3aa73ba
d9f2d33
a4dcebf
6d1844a
67ca0e6
94972ae
9287725
b4b89ca
6f0540d
11e9315
93ed88b
ac7607e
7ce5261
8dab7f4
4a91773
6f381df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def dda_line(x1, y1, x2, y2): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# Calculate the differences in x and y coordinates | ||
dx = x2 - x1 | ||
dy = y2 - y1 | ||
|
||
# Determine the number of steps to take | ||
steps = max(abs(dx), abs(dy)) | ||
|
||
# Calculate the increments for x and y | ||
x_increment = dx / steps | ||
y_increment = dy / steps | ||
|
||
# Lists to store the x and y coordinates of the line | ||
x_points = [x1] | ||
y_points = [y1] | ||
|
||
# Calculate and store the intermediate points | ||
x = x1 | ||
y = y1 | ||
for _ in range(steps): | ||
x += x_increment | ||
y += y_increment | ||
x_points.append(round(x)) | ||
y_points.append(round(y)) | ||
|
||
return x_points, y_points | ||
|
||
|
||
if __name__ == "__main__": | ||
# Input the coordinates of the two endpoints of the line | ||
x1 = int(input("Enter x1: ")) | ||
y1 = int(input("Enter y1: ")) | ||
x2 = int(input("Enter x2: ")) | ||
y2 = int(input("Enter y2: ")) | ||
|
||
# Calculate the points using DDA algorithm | ||
x_points, y_points = dda_line(x1, y1, x2, y2) | ||
|
||
# Plot the line using Matplotlib | ||
plt.plot(x_points, y_points, marker="o") | ||
plt.title("DDA Line Drawing Algorithm") | ||
plt.xlabel("X-axis") | ||
plt.ylabel("Y-axis") | ||
plt.grid() | ||
plt.show() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's name the file
digital_differential_analyzer_line.py
to try to keep it short