Advertisement
here2share

# tk_alias_line_draw.py

Feb 13th, 2024
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. # tk_alias_line_draw.py
  2.  
  3. import tkinter as tk
  4.  
  5. def draw_line(point_a, point_b):
  6.     num_squares = max(abs(point_b[0] - point_a[0]), abs(point_b[1] - point_a[1]))
  7.     square_size = 5
  8.  
  9.     # Draw the line using squares
  10.     for i in range(0, num_squares + 1, square_size):
  11.         x = point_a[0] + i * (point_b[0] - point_a[0]) / num_squares
  12.         y = point_a[1] + i * (point_b[1] - point_a[1]) / num_squares
  13.  
  14.         canvas.create_rectangle(x, y, x + square_size - 1, y + square_size - 1, outline="", fill="black")
  15.  
  16. # Example of usage
  17. root = tk.Tk()
  18. canvas = tk.Canvas(root, width=300, height=300)
  19. canvas.pack()
  20.  
  21. point_a = (100, 50)
  22. point_b = (250, 200)
  23.  
  24. draw_line(point_a, point_b)
  25.  
  26. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement