Advertisement
here2share

# Tk_rotate360adv.py

Feb 3rd, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. # Tk_rotate360adv.py
  2.  
  3. from Tkinter import *
  4. import math
  5.  
  6. WIDTH = 400
  7. HEIGHT = 400
  8. CANVAS_MID_X = WIDTH/2
  9. CANVAS_MID_Y = HEIGHT/2
  10.  
  11. root = Tk()
  12. canvas = Canvas(root, bg="grey", height=HEIGHT, width=WIDTH)
  13. canvas.pack()
  14.  
  15. def sqSize(SIDE=WIDTH/2):
  16.     return [
  17.     CANVAS_MID_X - SIDE/2, CANVAS_MID_Y - SIDE/2,
  18.     CANVAS_MID_X + SIDE/2, CANVAS_MID_Y - SIDE/2,
  19.     CANVAS_MID_X + SIDE/2, CANVAS_MID_Y + SIDE/2,
  20.     CANVAS_MID_X - SIDE/2, CANVAS_MID_Y + SIDE/2,
  21.     ]
  22. shape = {}
  23. shape['red'] = sqSize()
  24. shape['blue'] = sqSize(WIDTH/3)
  25.  
  26. def rotate(obj, angle, center):
  27.     points = shape[obj]
  28.     angle = math.radians(angle)
  29.     cos_val = math.cos(angle)
  30.     sin_val = math.sin(angle)
  31.     cx, cy = center
  32.     for_coords = []
  33.     while points:
  34.         x_old, y_old = points.pop(0), points.pop(0)
  35.         x_old -= cx
  36.         y_old -= cy
  37.         x_new = x_old * cos_val - y_old * sin_val
  38.         y_new = x_old * sin_val + y_old * cos_val
  39.         for_coords.append(x_new + cx)
  40.         for_coords.append(y_new + cy)
  41.     shape[obj] = for_coords
  42.  
  43. center = (CANVAS_MID_X, CANVAS_MID_Y)
  44. red_angle=-1
  45. blue_angle=15
  46. red_square = canvas.create_polygon(*shape['red'], fill="red")
  47. blue_square = canvas.create_polygon(*shape['blue'], fill="blue")
  48.  
  49. def mv():
  50.     red_coords = rotate('red', red_angle, center)
  51.     blue_coords = rotate('blue', blue_angle, center)
  52.     canvas.coords(red_square, *shape['red'])
  53.     canvas.coords(blue_square, *shape['blue'])
  54.     root.after(60, mv)
  55.  
  56. mv()
  57.  
  58. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement