Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions graphics/digital_differential_analyzer_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@ 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.

Args:
- p1: Coordinates of the starting point.
- p2: Coordinates of the ending point.
Returns:
- List of coordinate points that form the line.
Digital Differential Analyzer (DDA) Line Drawing Algorithm.

>>> digital_differential_analyzer_line((1, 1), (4, 4))
[(2, 2), (3, 3), (4, 4)]
This algorithm draws a straight line between two points by calculating
the difference in x (dx) and y (dy) coordinates and incrementally stepping
through the dominant axis while updating the other axis using fractional
increments.

One of the main disadvantages of the DDA algorithm is its reliance on
floating-point arithmetic, which can introduce rounding errors at each step.
Because of this, it is generally slower and less accurate than the
Bresenham line drawing algorithm, which uses only integer arithmetic.

Despite this, DDA is useful for educational purposes as it is simple
to understand and demonstrates the basic idea of incremental line generation.

References:
- https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)




Args:
- p1: Coordinates of the starting point.
- p2: Coordinates of the ending point.
Returns:
- List of coordinate points that form the line.

>>> digital_differential_analyzer_line((1, 1), (4, 4))
[(2, 2), (3, 3), (4, 4)]
"""
x1, y1 = p1
x2, y2 = p2
Expand Down