Skip to content

Commit c4915b4

Browse files
Bresenham line advanced algorithm which handles all octants (#13751)
* Added Bresenham Line Algorithm basic version for first quadrant * Add Generalized Bresenham Line Algorithm (all octants) for Hacktoberfest * updating DIRECTORY.md --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent d232045 commit c4915b4

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@
593593

594594
## [Graphics](graphics)
595595
* [Bezier Curve](graphics/bezier_curve.py)
596+
* [Bresenham Line Basic](graphics/bresenham_line_basic.py)
597+
* [Bresenham Line Generalized](graphics/bresenham_line_generalized.py)
596598
* [Butterfly Pattern](graphics/butterfly_pattern.py)
597599
* [Digital Differential Analyzer Line](graphics/digital_differential_analyzer_line.py)
598600
* [Vector3 For 2D Rendering](graphics/vector3_for_2d_rendering.py)

‎graphics/bresenham_line_basic.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Classic Bresenham's Line Drawing Algorithm
3+
------------------------------------------
4+
5+
Draws a line between two points (x1, y1) and (x2, y2)
6+
for slope 0 ≤ m ≤ 1, without floating-point operations.
7+
8+
Reference : https://www.geeksforgeeks.org/dsa/bresenhams-line-generation-algorithm/
9+
10+
>>> classic_bresenham_line((0, 0), (5, 3))
11+
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 2), (5, 3)]
12+
"""
13+
14+
import matplotlib.pyplot as plt
15+
16+
17+
def classic_bresenham_line(
18+
p1: tuple[int, int], p2: tuple[int, int]
19+
) -> list[tuple[int, int]]:
20+
x1, y1 = p1
21+
x2, y2 = p2
22+
23+
dx = x2 - x1
24+
dy = y2 - y1
25+
p = 2 * dy - dx
26+
y = y1
27+
points = []
28+
29+
for x in range(x1, x2 + 1):
30+
points.append((x, y))
31+
if p < 0:
32+
p += 2 * dy
33+
else:
34+
y += 1
35+
p += 2 * (dy - dx)
36+
return points
37+
38+
39+
if __name__ == "__main__":
40+
import doctest
41+
42+
doctest.testmod()
43+
44+
x1 = int(input("Enter x1: "))
45+
y1 = int(input("Enter y1: "))
46+
x2 = int(input("Enter x2: "))
47+
y2 = int(input("Enter y2: "))
48+
49+
points = classic_bresenham_line((x1, y1), (x2, y2))
50+
print("Generated points:", points)
51+
52+
xs, ys = zip(*points)
53+
plt.plot(xs, ys, marker="o")
54+
plt.title("Classic Bresenham's Line Algorithm")
55+
plt.grid()
56+
plt.show()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Generalized Bresenham's Line Drawing Algorithm
3+
----------------------------------------------
4+
5+
Handles all possible line slopes and directions.
6+
7+
Reference:
8+
https://www.geeksforgeeks.org/dsa/bresenhams-line-generation-algorithm/
9+
10+
>>> generalized_bresenham_line((0, 0), (5, 3))
11+
[(0, 0), (1, 1), (2, 1), (3, 2), (4, 2), (5, 3)]
12+
>>> generalized_bresenham_line((5, 5), (2, 3))
13+
[(5, 5), (4, 4), (3, 4), (2, 3)]
14+
"""
15+
16+
import matplotlib.pyplot as plt
17+
18+
19+
def generalized_bresenham_line(
20+
p1: tuple[int, int], p2: tuple[int, int]
21+
) -> list[tuple[int, int]]:
22+
x1, y1 = p1
23+
x2, y2 = p2
24+
dx = abs(x2 - x1)
25+
dy = abs(y2 - y1)
26+
sx = 1 if x1 < x2 else -1
27+
sy = 1 if y1 < y2 else -1
28+
err = dx - dy
29+
30+
points = []
31+
32+
while True:
33+
points.append((x1, y1))
34+
if x1 == x2 and y1 == y2:
35+
break
36+
e2 = 2 * err
37+
if e2 > -dy:
38+
err -= dy
39+
x1 += sx
40+
if e2 < dx:
41+
err += dx
42+
y1 += sy
43+
44+
return points
45+
46+
47+
if __name__ == "__main__":
48+
import doctest
49+
50+
doctest.testmod()
51+
52+
x1 = int(input("Enter x1: "))
53+
y1 = int(input("Enter y1: "))
54+
x2 = int(input("Enter x2: "))
55+
y2 = int(input("Enter y2: "))
56+
57+
points = generalized_bresenham_line((x1, y1), (x2, y2))
58+
print("Generated points:", points)
59+
60+
xs, ys = zip(*points)
61+
plt.plot(xs, ys, marker="o")
62+
plt.title("Generalized Bresenham's Line Algorithm (All Slopes)")
63+
plt.grid()
64+
plt.show()

0 commit comments

Comments
 (0)