-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
174 lines (126 loc) · 5.64 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import pygame
import math
pygame.init()
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
RED = [255, 0, 0]
BLUE = [0, 0, 255]
GREEN = [0, 255, 0]
PI = math.pi
class doublePendulum():
def __init__(self, theta1, theta2, color):
self.theta1 = theta1
self.theta2 = theta2
self.R = color[0]
self.G = color[1]
self.B = color[2]
self.x1 = 0
self.y1 = 0
self.x2 = 0
self.y2 = 0
self.endPoints = []
self.l1 = 140
self.l2 = 40
self.m1 = 0.1
self.m2 = 0.000001
self.v1 = 0
self.v2 = 0
self.a1 = 0
self.a2 = 0
self.g = 1
self.color = color
self.screen = pygame.display.get_surface()
self.width, self.height = self.screen.get_size()
#Calculating first rod position
def __firstPosition(self):
self.x1 = self.l1 * math.sin(self.theta1) + self.width / 2
self.y1 = self.l1 * math.cos(self.theta1) + self.height / 4
#Calculating second rod position
def __secondPosition(self):
self.x2 = self.l1 * math.sin(self.theta1) + self.l2 * math.sin(self.theta2) + self.width / 2
self.y2 = self.l1 * math.cos(self.theta1) + self.l2 * math.cos(self.theta2) + self.height / 4
#Angular acceleration of theta 1
def __acceleration1(self):
exp1 = -self.g * (2 * self.m1 + self.m2) * math.sin(self.theta1)
exp2 = self.m2 * self.g * math.sin(self.theta1 - 2 * self.theta2)
exp3 = 2 * math.sin(self.theta1 - self.theta2) * self.m2 * ((self.v2 * self.v2) * self.l2 + (self.v1 * self.v1) * self.l1 * math.cos(self.theta1 - self.theta2))
exp4 = self.l1 * (2 * self.m1 + self.m2 - self.m2 * math.cos(2 * self.theta1 - 2 * self.theta2))
self.a1 = (exp1 - exp2 - exp3) / (exp4)
#Angular acceleration of theta 2
def __acceleration2(self):
exp1 = 2 * math.sin(self.theta1 - self.theta2)
exp2 = (self.v1 * self.v1) * self.l1 * (self.m1 + self.m2)
exp3 = self.g * (self.m1 + self.m2) * math.cos(self.theta1)
exp4 = (self.v2 * self.v2) * self.l2 * self.m2 * math.cos(self.theta1 - self.theta2)
exp5 = self.l2 * (2 * self.m1 + self.m2 - self.m2 * math.cos(2 * self.theta1 - 2 * self.theta2))
self.a2 = (exp1 * (exp2 + exp3 + exp4)) / (exp5)
#Draw everything
def draw(self):
pygame.draw.circle(self.screen, self.color, (int(self.x1), int(self.y1)), 12)
pygame.draw.line(self.screen, BLACK, (int(self.width / 2), int(self.height / 4)), (int(self.x1), int(self.y1)), 1)
pygame.draw.circle(self.screen, self.color, (int(self.x2), int(self.y2)), 12)
pygame.draw.line(self.screen, BLACK, (int(self.x1), int(self.y1)), (int(self.x2), int(self.y2)), 1)
#Operating pendulum (set positions and angles)
def run(self):
self.__firstPosition()
self.__secondPosition()
self.endPoints.append((self.x2, self.y2))
self.__acceleration1()
self.__acceleration2()
self.v1 += self.a1
self.v2 += self.a2
self.theta1 += self.v1
self.theta2 += self.v2
#Trace the end points (opt 1)
def trace(self):
for i in range(len(self.endPoints) - 1):
pygame.draw.line(self.screen, (self.R, self.G, self.B), (int(self.endPoints[i][0]), int(self.endPoints[i][1])),
(int(self.endPoints[i + 1][0]), int(self.endPoints[i + 1][1])), 2)
#Shade the arc (opt 2)
def shade(self):
if len(self.endPoints) > 30:
self.endPoints.remove(self.endPoints[0])
for i in range(len(self.endPoints) - 1):
pygame.draw.line(self.screen, (self.R, self.G, self.B), (int(self.endPoints[i][0]), int(self.endPoints[i][1])),
(int(self.x2), int(self.y2)), 1)
#End points fade away (opt 3)
def fade(self):
if len(self.endPoints) > 10:
self.endPoints.remove(self.endPoints[0])
for i in range(len(self.endPoints) - 1):
R = self.R + (len(self.endPoints) - i) * 25
G = self.G + (len(self.endPoints) - i) * 25
B = self.B + (len(self.endPoints) - i) * 25
if R > 255:
R = 255
if G > 255:
G = 255
if B > 255:
B = 255
pygame.draw.line(self.screen, (R, G, B), (int(self.endPoints[i][0]), int(self.endPoints[i][1])),
(int(self.endPoints[i + 1][0]), int(self.endPoints[i + 1][1])), 10)
def main():
w = pygame.display.set_mode((600, 400))
w.fill(WHITE)
view = True
font = pygame.font.SysFont(None, 20)
clock = pygame.time.Clock()
pendulum1 = doublePendulum(PI / 2, PI / 8, BLACK)
pendulum2 = doublePendulum(PI / 2, PI / 3, BLUE)
pendulum3 = doublePendulum(PI / 2, PI / 4, RED)
while view:
for event in pygame.event.get():
if event.type == pygame.QUIT:
view = False
pendulum1.run()
pendulum1.trace()
pendulum1.draw()
acc1 = font.render("First Rod: " + str(format(pendulum1.a1, '.3f')) + " rad/s^2", 0, BLACK)
acc2 = font.render("Second Rod: " + str(format(pendulum1.a2, '.3f')) + " rad/s^2", 0, BLACK)
w.blit(acc1, (10, 10))
w.blit(acc2, (10, 50))
clock.tick(50)
pygame.display.flip()
w.fill(WHITE)
if __name__ == "__main__":
main()