This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
121 lines (94 loc) · 2.77 KB
/
graph.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import math
import sys
import time
import random
from graphics import *
import numpy as np
import math
from QsuiteGui import norm
win = GraphWin('Pendulum',height=1000,width=1000,autoflush=True)
win.setBackground('white')
rectwidth = 20
rectlength = 500
p0 = np.array((50,50))
p1 = np.array((50+rectlength,50+rectwidth))
rect = Rectangle(Point(50,50),Point(50+rectlength,50+rectwidth))
rect.setFill('blue')
rect.draw(win)
midPoint = np.array(( (p0[0] + p1[0])/2, (p0[1]+p1[1]+rectwidth)/2 ))
centre = np.array((300,300))
v1 = p1 - midPoint
v2 = centre - midPoint
angle = math.acos( np.dot(v1,v2)/(norm(v1,len(v1)) * norm(v2,len(v2))) )
startTime = time.time()
g = 9.81
T = 2*math.pi*math.sqrt(norm(v2,len(v2))/g)
w = 2*math.pi/T
angleState = False
line = Line(Point(midPoint[0],midPoint[1]),Point(centre[0],centre[1]))
circ = Circle(Point(centre[0],centre[1]),5)
circ.setFill('blue')
circ.draw(win)
line.setFill('blue')
# line.setWidth(20)
line.draw(win)
text = Text(Point(900,50),'Test')
text.setFill('green')
text.setSize(10)
text.setStyle('italic')
text.setFace('arial')
text.draw(win)
epochTime = time.time()
Oscillations = 0
while(True):
elapsedTime = time.time() - startTime
angle = angle + w * elapsedTime
startTime = time.time()
line.undraw()
circ.undraw()
if(angle >= math.pi):
angleState = True
while(angleState):
# line.undraw()
# circ.undraw()
elapsedTime = time.time() - startTime
angle = angle - w * elapsedTime
startTime = time.time()
# line.undraw()
# circ.undraw()
dx = norm(v2,len(v2)) * math.cos(angle)
dy = norm(v2,len(v2)) * math.sin(angle)
x = centre[0] + dx
y = centre[1] + dy
# time.sleep(0.05)
line = Line(Point(midPoint[0],midPoint[1]),Point(x,y))
circ = Circle(Point(x,y),20)
circ.setFill('blue')
circ.draw(win)
win.plot(x,y,'blue')
line.setFill('blue')
line.setWidth(5)
line.draw(win)
text.setText('Duration: {}s\nno Oscillations: {}\nPeriod: {}'.format(time.time()-epochTime,Oscillations,T))
line.undraw()
circ.undraw()
win.update()
if(angle <= 0):
angleState = False
Oscillations += 1
break
dx = norm(v2,len(v2)) * math.cos(angle)
dy = norm(v2,len(v2)) * math.sin(angle)
x = centre[0] + dx
y = centre[1] + dy
# time.sleep(0.05)
line = Line(Point(midPoint[0],midPoint[1]),Point(x,y))
circ = Circle(Point(x,y),20)
circ.setFill('blue')
circ.draw(win)
win.plot(x,y,'red')
line.setFill('blue')
line.setWidth(5)
text.setText('Duration: {}s\nno Oscillations: {}\nPeriod: {}'.format(time.time()-epochTime,Oscillations,T))
line.draw(win)
win.update()