-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotateCanvas.py
106 lines (91 loc) · 2.89 KB
/
rotateCanvas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from tkinter import *
import math
from random import randint
from time import sleep
WIDTH = 800
HEIGHT = 800
CANVAS_MID_X = WIDTH/2
CANVAS_MID_Y = HEIGHT/2
SIDE = WIDTH/8
lastKnownCursor = [0,0]
root = Tk()
canvas = Canvas(root, bg="black", height=HEIGHT, width=WIDTH)
canvas.pack()
vertices = [
[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],
]
def rotate(points, angle, center):
moved_points = updatePosition(center)
angle = math.radians(angle)
cos_val = math.cos(angle)
sin_val = math.sin(angle)
cx, cy = center
new_points = []
for x_old, y_old in moved_points:
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
new_points.append([x_new + cx, y_new + cy])
return new_points
def draw_square(points, color="red"):
canvas.delete("all")
canvas.create_polygon(points, fill=color)
def updateDirection(event):
global center,lastKnownCursor
if event == "DIRECTION":
x = lastKnownCursor[0]
y = lastKnownCursor[1]
else:
x, y = event.x, event.y
lastKnownCursor = [x,y]
X = center[0]
Y = center[1]
if (abs(y-Y) != 0) and (abs(x-X) != 0):
if ((x > X) and (Y > y)) or ((X > x) and (y > Y)):
angle = - math.degrees(math.atan( (abs(y-Y) / abs(x-X)) ))
else:
angle = math.degrees(math.atan( (abs(y-Y) / abs(x-X)) ))
new_square = rotate(vertices, angle, center)
draw_square(new_square)
canvas.create_line(X,Y,x,y, fill="blue")
angleOld = angle
def updatePosition(center):
moved_points = []
moved_points.append([center[0]-SIDE,center[1]-SIDE])
moved_points.append([center[0]-SIDE,center[1]+SIDE])
moved_points.append([center[0]+SIDE,center[1]+SIDE])
moved_points.append([center[0]+SIDE,center[1]-SIDE])
return(moved_points)
def move(letter):
global center,lastKnownCursor
if letter == "w":
center = [center[0],center[1]-10]
elif letter == "s":
center = [center[0],center[1]+10]
elif letter == "a":
center = [center[0]-10,center[1]]
elif letter == "d":
center = [center[0]+10,center[1]]
updateDirection("DIRECTION")
center = (CANVAS_MID_X, CANVAS_MID_Y)
center = (400.0,400.0)
new_square = rotate(vertices, 0, center)
draw_square(new_square)
root.bind('<Motion>', updateDirection)
root.bind('w', lambda x: move('w'))
root.bind('s', lambda x: move('s'))
root.bind('a', lambda x: move('a'))
root.bind('d', lambda x: move('d'))
#root.update()
#angle=180
#while True:
# angle = input("Enter Angle\n")
# print(angle)
# new_square = rotate(vertices, float(angle), center)
# draw_square(new_square)
# root.update()
mainloop()