Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_mouse2rotate.py
- from Tkinter import *
- import math
- import time
- root = Tk()
- root.title("Tk mouse2rotate")
- ww=200; hh=200
- z = 0
- spin = {}
- while z < 1000:
- x = int(math.sin(math.radians(z))*1000.0)
- y = int(math.cos(math.radians(z))*1000.0)
- spin[z] = x,y
- z += 1
- canvas = Canvas(width=ww, height=hh)
- canvas.pack()
- # a square
- xy = [(50, 50), (150, 50), (150, 150), (50, 150)]
- polygon_item = canvas.create_polygon(xy)
- center = ww/2, hh/2
- def getangle(event):
- dx = canvas.canvasx(event.x) - center[0]
- dy = canvas.canvasy(event.y) - center[1]
- print dx,dy
- try:
- return complex(dx, dy) / abs(complex(dx, dy))
- except ZeroDivisionError:
- return 0.0 # cannot determine angle
- def getspinangle():
- x,y = spin[z]
- dx = x - center[0]
- dy = y - center[1]
- return complex(dx, dy) / abs(complex(dx, dy))
- def press(event):
- # calculate angle at start point
- global start
- start = getangle(event)
- def motion(event):
- # calculate current angle relative to initial angle
- global start, delay
- delay = time.clock()+1
- angle = getangle(event) / start
- offset = complex(center[0], center[1])
- newxy = []
- for x, y in xy:
- v = angle * (complex(x, y) - offset) + offset
- newxy.append(v.real)
- newxy.append(v.imag)
- canvas.itemconfig(polygon_item, fill='green')
- canvas.coords(polygon_item, *newxy)
- canvas.bind("<Button-1>", press)
- canvas.bind("<B1-Motion>", motion)
- z = 0
- Lz = len(spin)
- delay = 0
- while 1:
- if delay < time.clock():
- offset = complex(center[0], center[1])
- newxy = []
- angle = getspinangle()
- z += 1
- z = z%Lz
- for x, y in xy:
- v = angle * (complex(x, y) - offset) + offset
- newxy.append(v.real)
- newxy.append(v.imag)
- canvas.itemconfig(polygon_item, fill='orange')
- canvas.coords(polygon_item, *newxy)
- delay = time.clock()+0.1
- canvas.update()
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement