Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_rotate360adv.py
- from Tkinter import *
- import math
- WIDTH = 400
- HEIGHT = 400
- CANVAS_MID_X = WIDTH/2
- CANVAS_MID_Y = HEIGHT/2
- root = Tk()
- canvas = Canvas(root, bg="grey", height=HEIGHT, width=WIDTH)
- canvas.pack()
- def sqSize(SIDE=WIDTH/2):
- return [
- CANVAS_MID_X - SIDE/2, CANVAS_MID_Y - SIDE/2,
- CANVAS_MID_X + SIDE/2, CANVAS_MID_Y - SIDE/2,
- CANVAS_MID_X + SIDE/2, CANVAS_MID_Y + SIDE/2,
- CANVAS_MID_X - SIDE/2, CANVAS_MID_Y + SIDE/2,
- ]
- shape = {}
- shape['red'] = sqSize()
- shape['blue'] = sqSize(WIDTH/3)
- def rotate(obj, angle, center):
- points = shape[obj]
- angle = math.radians(angle)
- cos_val = math.cos(angle)
- sin_val = math.sin(angle)
- cx, cy = center
- for_coords = []
- while points:
- x_old, y_old = points.pop(0), points.pop(0)
- x_old -= cx
- y_old -= cy
- x_new = x_old * cos_val - y_old * sin_val
- y_new = x_old * sin_val + y_old * cos_val
- for_coords.append(x_new + cx)
- for_coords.append(y_new + cy)
- shape[obj] = for_coords
- center = (CANVAS_MID_X, CANVAS_MID_Y)
- red_angle=-1
- blue_angle=15
- red_square = canvas.create_polygon(*shape['red'], fill="red")
- blue_square = canvas.create_polygon(*shape['blue'], fill="blue")
- def mv():
- red_coords = rotate('red', red_angle, center)
- blue_coords = rotate('blue', blue_angle, center)
- canvas.coords(red_square, *shape['red'])
- canvas.coords(blue_square, *shape['blue'])
- root.after(60, mv)
- mv()
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement