fork download
  1. import math
  2.  
  3. def draw_line_dda(x1, y1, x2, y2):
  4. # 1. Calculate delx and dely
  5. delx = x2 - x1
  6. dely = y2 - y1
  7.  
  8. # 2. Find length
  9. length = max(abs(delx), abs(dely))
  10.  
  11. # 3. Calculate step increments
  12. x_inc = delx / length
  13. y_inc = dely / length
  14.  
  15. # Initialize coordinates
  16. x, y = x1, y1
  17.  
  18. # 4. Step loop
  19. for _ in range(int(length) + 1):
  20. print(f"Pixel at: ({round(x)}, {round(y)})")
  21. x += x_inc
  22. y += y_inc
  23.  
  24. # Example usage
  25. draw_line_dda(2, 3, 9, 8)
Success #stdin #stdout 0.08s 14000KB
stdin
Standard input is empty
stdout
Pixel at: (2, 3)
Pixel at: (3, 4)
Pixel at: (4, 4)
Pixel at: (5, 5)
Pixel at: (6, 6)
Pixel at: (7, 7)
Pixel at: (8, 7)
Pixel at: (9, 8)