From 99a13d4f3be558e6abfb2278df96da7b8248eaf8 Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Sat, 25 Oct 2025 15:44:04 +0530 Subject: [PATCH 1/3] Added Bresenham Line Algorithm basic version for first quadrant --- graphics/bresenham_line_basic.py | 56 ++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 graphics/bresenham_line_basic.py diff --git a/graphics/bresenham_line_basic.py b/graphics/bresenham_line_basic.py new file mode 100644 index 000000000000..6e9120985624 --- /dev/null +++ b/graphics/bresenham_line_basic.py @@ -0,0 +1,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() From 71ad663aa699476e6289a3c3de7cd499c0dded86 Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Sat, 25 Oct 2025 16:00:46 +0530 Subject: [PATCH 2/3] Add Generalized Bresenham Line Algorithm (all octants) for Hacktoberfest --- graphics/bresenham_line_generalized.py | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 graphics/bresenham_line_generalized.py diff --git a/graphics/bresenham_line_generalized.py b/graphics/bresenham_line_generalized.py new file mode 100644 index 000000000000..635951736a41 --- /dev/null +++ b/graphics/bresenham_line_generalized.py @@ -0,0 +1,64 @@ +""" +Generalized Bresenham's Line Drawing Algorithm +---------------------------------------------- + +Handles all possible line slopes and directions. + +Reference: +https://www.geeksforgeeks.org/dsa/bresenhams-line-generation-algorithm/ + +>>> generalized_bresenham_line((0, 0), (5, 3)) +[(0, 0), (1, 1), (2, 1), (3, 2), (4, 2), (5, 3)] +>>> generalized_bresenham_line((5, 5), (2, 3)) +[(5, 5), (4, 4), (3, 4), (2, 3)] +""" + +import matplotlib.pyplot as plt + + +def generalized_bresenham_line( + p1: tuple[int, int], p2: tuple[int, int] +) -> list[tuple[int, int]]: + x1, y1 = p1 + x2, y2 = p2 + dx = abs(x2 - x1) + dy = abs(y2 - y1) + sx = 1 if x1 < x2 else -1 + sy = 1 if y1 < y2 else -1 + err = dx - dy + + points = [] + + while True: + points.append((x1, y1)) + if x1 == x2 and y1 == y2: + break + e2 = 2 * err + if e2 > -dy: + err -= dy + x1 += sx + if e2 < dx: + err += dx + y1 += sy + + 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 = generalized_bresenham_line((x1, y1), (x2, y2)) + print("Generated points:", points) + + xs, ys = zip(*points) + plt.plot(xs, ys, marker="o") + plt.title("Generalized Bresenham's Line Algorithm (All Slopes)") + plt.grid() + plt.show() From 5e3b03140c5947d528bd14794e46f2be6f816dde Mon Sep 17 00:00:00 2001 From: devdandekar24 Date: Sat, 25 Oct 2025 16:13:51 +0530 Subject: [PATCH 3/3] Add midpoint Line Drawing Algorithm --- graphics/midpoint_line.py | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 graphics/midpoint_line.py diff --git a/graphics/midpoint_line.py b/graphics/midpoint_line.py new file mode 100644 index 000000000000..64b34f31faef --- /dev/null +++ b/graphics/midpoint_line.py @@ -0,0 +1,78 @@ +""" +Midpoint Line Drawing Algorithm +------------------------------- + +Handles all lines with arbitrary slopes using the midpoint method. + +Referece: +https://www.geeksforgeeks.org/dsa/mid-point-line-generation-algorithm/ + +>>> midpoint_line((0, 0), (5, 3)) +[(0, 0), (1, 1), (2, 1), (3, 2), (4, 2), (5, 3)] +>>> midpoint_line((5, 5), (2, 3)) +[(5, 5), (4, 4), (3, 4), (2, 3)] +""" + +import matplotlib.pyplot as plt + + +def midpoint_line(p1: tuple[int, int], p2: tuple[int, int]) -> list[tuple[int, int]]: + x1, y1 = p1 + x2, y2 = p2 + + points = [] + + dx = x2 - x1 + dy = y2 - y1 + + # Determine the direction of the step + sx = 1 if dx >= 0 else -1 + sy = 1 if dy >= 0 else -1 + + dx = abs(dx) + dy = abs(dy) + + if dx > dy: + # Shallow slope: iterate over x + d = dy - dx / 2 + x, y = x1, y1 + while x != x2 + sx: + points.append((x, y)) + if d >= 0: + y += sy + d -= dx + x += sx + d += dy + else: + # Steep slope: iterate over y + d = dx - dy / 2 + x, y = x1, y1 + while y != y2 + sy: + points.append((x, y)) + if d >= 0: + x += sx + d -= dy + y += sy + d += 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 = midpoint_line((x1, y1), (x2, y2)) + print("Generated points:", points) + + xs, ys = zip(*points) + plt.plot(xs, ys, marker="o") + plt.title("Midpoint Line Drawing Algorithm") + plt.grid() + plt.show()