From 566350d6621b1a55ab40d21fe8d1a19516e5d242 Mon Sep 17 00:00:00 2001 From: Ranjita1708 <153986147+Ranjita1708@users.noreply.github.com> Date: Sun, 16 Nov 2025 06:59:37 +0000 Subject: [PATCH 1/3] fix(dda): fix docstring --- .../digital_differential_analyzer_line.py | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/graphics/digital_differential_analyzer_line.py b/graphics/digital_differential_analyzer_line.py index f7269ab09856..91e0a0899260 100644 --- a/graphics/digital_differential_analyzer_line.py +++ b/graphics/digital_differential_analyzer_line.py @@ -5,16 +5,31 @@ def digital_differential_analyzer_line( p1: tuple[int, int], p2: tuple[int, int] ) -> list[tuple[int, int]]: """ - Draws a line between two points using the DDA algorithm. + Draws a line between two points using the Digital Differential Analyzer (DDA) algorithm. + + The DDA algorithm works by computing the differences in x and y (dx, dy), + determining the dominant axis (the one with the larger absolute difference), + and incrementing along it in small uniform steps. The other coordinate is + updated by a proportional fractional amount at each iteration, producing a + sequence of intermediate points that approximate a straight line. + + While simple and widely used for teaching computer graphics, the algorithm + relies on floating-point arithmetic and rounding at each step. This makes it + less efficient and less precise than the integer-only Bresenham line algorithm, + which is commonly preferred in performance-critical rasterization tasks. Args: - - p1: Coordinates of the starting point. - - p2: Coordinates of the ending point. + p1: Coordinates of the starting point. + p2: Coordinates of the ending point. + Returns: - - List of coordinate points that form the line. + List of coordinate points that form the line. >>> digital_differential_analyzer_line((1, 1), (4, 4)) [(2, 2), (3, 3), (4, 4)] + + For more information, see: + https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm) """ x1, y1 = p1 x2, y2 = p2 From 244225fbc2374835e6c10c73a80cbb1f6b75cca2 Mon Sep 17 00:00:00 2001 From: Ranjita1708 <153986147+Ranjita1708@users.noreply.github.com> Date: Sun, 16 Nov 2025 07:11:32 +0000 Subject: [PATCH 2/3] style(dda): wrap docstring lines to satisfy ruff E501 --- .../digital_differential_analyzer_line.py | 77 ++++++++----------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/graphics/digital_differential_analyzer_line.py b/graphics/digital_differential_analyzer_line.py index 91e0a0899260..a1e1eb6a5029 100644 --- a/graphics/digital_differential_analyzer_line.py +++ b/graphics/digital_differential_analyzer_line.py @@ -1,67 +1,56 @@ -import matplotlib.pyplot as plt +from typing import List, Tuple def digital_differential_analyzer_line( - p1: tuple[int, int], p2: tuple[int, int] -) -> list[tuple[int, int]]: + p1: Tuple[int, int], p2: Tuple[int, int] +) -> List[Tuple[int, int]]: """ - Draws a line between two points using the Digital Differential Analyzer (DDA) algorithm. + Draw a line between two points using the Digital Differential Analyzer (DDA) + algorithm. - The DDA algorithm works by computing the differences in x and y (dx, dy), - determining the dominant axis (the one with the larger absolute difference), - and incrementing along it in small uniform steps. The other coordinate is - updated by a proportional fractional amount at each iteration, producing a - sequence of intermediate points that approximate a straight line. - - While simple and widely used for teaching computer graphics, the algorithm - relies on floating-point arithmetic and rounding at each step. This makes it - less efficient and less precise than the integer-only Bresenham line algorithm, - which is commonly preferred in performance-critical rasterization tasks. + The DDA algorithm increments the dominant axis in unit steps and updates the + other axis proportionally. After each step coordinates are rounded to the + nearest integer to obtain pixel coordinates. Args: - p1: Coordinates of the starting point. - p2: Coordinates of the ending point. + p1: Starting coordinate (x1, y1). + p2: Ending coordinate (x2, y2). Returns: - List of coordinate points that form the line. + A list of integer coordinate tuples representing the rasterized line. + The list includes the end point and excludes the start point. + Examples: >>> digital_differential_analyzer_line((1, 1), (4, 4)) [(2, 2), (3, 3), (4, 4)] - For more information, see: - https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm) + >>> digital_differential_analyzer_line((0, 0), (0, 5)) + [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)] + + >>> digital_differential_analyzer_line((5, 5), (2, 5)) + [(4, 5), (3, 5), (2, 5)] + + >>> digital_differential_analyzer_line((3, 3), (3, 3)) + [(3, 3)] """ x1, y1 = p1 x2, y2 = p2 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((round(x), round(y))) - return coordinates + if steps == 0: + return [p1] + x_inc = dx / float(steps) + y_inc = dy / float(steps) -if __name__ == "__main__": - import doctest + x, y = float(x1), float(y1) + points: List[Tuple[int, int]] = [] - doctest.testmod() + for _ in range(steps): + x += x_inc + y += y_inc + points.append((round(x), round(y))) - 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() + return points From b4c6a00ed4cf512b241416fe9afe1919a81caabf Mon Sep 17 00:00:00 2001 From: Ranjita1708 <153986147+Ranjita1708@users.noreply.github.com> Date: Sun, 16 Nov 2025 07:23:25 +0000 Subject: [PATCH 3/3] style(dda): fix docstring --- graphics/digital_differential_analyzer_line.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/graphics/digital_differential_analyzer_line.py b/graphics/digital_differential_analyzer_line.py index a1e1eb6a5029..92fcb4ec1e74 100644 --- a/graphics/digital_differential_analyzer_line.py +++ b/graphics/digital_differential_analyzer_line.py @@ -1,9 +1,6 @@ -from typing import List, Tuple - - def digital_differential_analyzer_line( - p1: Tuple[int, int], p2: Tuple[int, int] -) -> List[Tuple[int, int]]: + p1: tuple[int, int], p2: tuple[int, int] +) -> list[tuple[int, int]]: """ Draw a line between two points using the Digital Differential Analyzer (DDA) algorithm. @@ -46,7 +43,7 @@ def digital_differential_analyzer_line( y_inc = dy / float(steps) x, y = float(x1), float(y1) - points: List[Tuple[int, int]] = [] + points: list[tuple[int, int]] = [] for _ in range(steps): x += x_inc