fork download
  1. import matplotlib.pyplot as plt
  2. def draw_line(x1, y1, x2, y2):
  3. dx = abs(x2 - x1)
  4. dy = abs(y2 - y1)
  5. steep = dy > dx
  6. if steep:
  7. x1, y1 = y1, x1
  8. x2, y2 = y2, x2
  9. dx, dy = dy, dx
  10. if x1 > x2:
  11. x1, x2 = x2, x1
  12. y1, y2 = y2, y1
  13. p = 2 * dy - dx
  14. y = y1
  15. points = []
  16. for x in range(x1, x2 + 1):
  17. if steep:
  18. points.append((y, x))
  19. else:
  20. points.append((x, y))
  21. if p >= 0:
  22. y += 1 if y1 < y2 else -1
  23. p -= 2 * dx
  24. p += 2 * dy
  25. return points
  26.  
  27. def main():
  28. x1, y1 = map(int, input("Enter starting point (x1 y1): ").split())
  29. x2, y2 = map(int, input("Enter ending point (x2 y2): ").split())
  30. points = draw_line(x1, y1, x2, y2)
  31. x_values = [point[0] for point in points]
  32. y_values = [point[1] for point in points]
  33.  
  34. plt.plot(x_values, y_values, marker='0')
  35. plt.xlabel('X-axis')
  36. plt.ylabel('Y-axis')
  37. plt.title('Bresenham Line Drawing Algorithm')
  38. plt.grid()
  39. plt.show()
  40.  
  41. if name == " main ":
  42. main()
  43.  
Success #stdin #stdout 2.18s 53636KB
stdin
Standard input is empty
stdout
Standard output is empty