Advertisement
here2share

# Tk_rotate360.py

Feb 3rd, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. # Tk_rotate360.py
  2.  
  3. from Tkinter import *
  4.  
  5. import math
  6.  
  7. c = Canvas(width=200, height=200)
  8. c.pack()
  9.  
  10. # a square
  11. xy = [(50, 50), (150, 50), (150, 150), (50, 150)]
  12.  
  13. polygon_item = c.create_polygon(xy)
  14.  
  15. center = 100, 100
  16.  
  17. def getangle(event):
  18.     dx = c.canvasx(event.x) - center[0]
  19.     dy = c.canvasy(event.y) - center[1]
  20.     try:
  21.         return complex(dx, dy) / abs(complex(dx, dy))
  22.     except ZeroDivisionError:
  23.         return 0.0 # cannot determine angle
  24.  
  25. def press(event):
  26.     # calculate angle at start point
  27.     global start
  28.     start = getangle(event)
  29.  
  30. def motion(event):
  31.     # calculate current angle relative to initial angle
  32.     global start
  33.     angle = getangle(event) / start
  34.     offset = complex(center[0], center[1])
  35.     newxy = []
  36.     for x, y in xy:
  37.         v = angle * (complex(x, y) - offset) + offset
  38.         newxy.append(v.real)
  39.         newxy.append(v.imag)
  40.     c.coords(polygon_item, *newxy)
  41.  
  42. c.bind("<Button-1>", press)
  43. c.bind("<B1-Motion>", motion)
  44.  
  45. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement