-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathbresenham_line_basic.py
More file actions
56 lines (42 loc) · 1.24 KB
/
bresenham_line_basic.py
File metadata and controls
56 lines (42 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Classic Bresenham's Line Drawing Algorithm
------------------------------------------
Draws a line between two points (x1, y1) and (x2, y2)
for slope 0 ≤ m ≤ 1, without floating-point operations.
Reference : https://www.geeksforgeeks.org/dsa/bresenhams-line-generation-algorithm/
>>> classic_bresenham_line((0, 0), (5, 3))
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 2), (5, 3)]
"""
import matplotlib.pyplot as plt
def classic_bresenham_line(
p1: tuple[int, int], p2: tuple[int, int]
) -> list[tuple[int, int]]:
x1, y1 = p1
x2, y2 = p2
dx = x2 - x1
dy = y2 - y1
p = 2 * dy - dx
y = y1
points = []
for x in range(x1, x2 + 1):
points.append((x, y))
if p < 0:
p += 2 * dy
else:
y += 1
p += 2 * (dy - dx)
return points
if __name__ == "__main__":
import doctest
doctest.testmod()
x1 = int(input("Enter x1: "))
y1 = int(input("Enter y1: "))
x2 = int(input("Enter x2: "))
y2 = int(input("Enter y2: "))
points = classic_bresenham_line((x1, y1), (x2, y2))
print("Generated points:", points)
xs, ys = zip(*points)
plt.plot(xs, ys, marker="o")
plt.title("Classic Bresenham's Line Algorithm")
plt.grid()
plt.show()