-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (158 loc) · 5.04 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import math as m
from pyfiglet import Figlet
from alive_progress import alive_bar
import random
def defAndClear():
global debugFlag
global randomFlag
global forceDict
global radDict
global forceKey
global radKey
global xDict
global yDict
global xKey
global yKey
global forceSum
global forceSumAngle
global xSum
global ySum
global xDictValues
global yDictValues
randomFlag = False
debugFlag = False
forceDict = {}
radDict = {}
forceKey = "force{}"
radKey = "angle{}"
xDict = {}
yDict = {}
xKey = "x{}"
yKey = "y{}"
forceSum = 0.0
forceSumAngle = 0.0
xSum = 0.0
ySum = 0.0
xDictValues = {}
yDictValues = {}
def renderAscii(msg):
terminal = os.get_terminal_size()
welcome_fig = Figlet(
font="kban", justify="left", width=getattr(terminal, "columns")
)
print()
print(welcome_fig.renderText(msg))
print("By: PEOL0", end="\n\n")
print("Remember the math!", end="\n\n")
def inputForces():
forces = input("Antal krafter: ").split()
if "d" in forces:
global debugFlag
debugFlag = True
if "r" in forces:
global randomFlag
randomFlag = True
if randomFlag == True:
with alive_bar(int(forces[0])) as bar:
for each in range(int(forces[0])):
inputPrompt(each)
bar()
else:
for each in range(int(forces[0])):
inputPrompt(each)
if debugFlag == True:
print(forceDict)
print(radDict)
def inputPrompt(each):
newforce = random.uniform(-100, 100)
if randomFlag != True:
promt = "Storlek{}: "
newforce = float(eval(input(promt.format(each + 1))))
forceDict.update({forceKey.format(each + 1): newforce})
if debugFlag == True:
print(
"Uppdaterat " + forceKey.format(each + 1) + " med värdet: " + str(newforce)
)
newangle = m.radians(random.uniform(0, 360))
if randomFlag != True:
prompt = "Vinkel{}: "
newangle = m.radians(float(eval(input(prompt.format(each + 1)))))
if newangle < 0:
newangle = m.radians(360 + m.degrees(newangle))
if newforce < 0:
radDict.update({radKey.format(each + 1): newangle + m.radians(180)})
else:
radDict.update({radKey.format(each + 1): newangle})
if debugFlag == True:
print("Uppdaterat " + radKey.format(each + 1) + " med värdet: " + str(newangle))
def split():
with alive_bar(len(forceDict) * 2) as bar:
for each in range(len(forceDict)):
forceDictValues = list(forceDict.values())
angleDictValues = list(radDict.values())
xComponentSplit(each, forceDictValues[each], angleDictValues[each])
bar()
yComponentSplit(each, forceDictValues[each], angleDictValues[each])
bar()
def xComponentSplit(index, force, rad):
xComponent = round(abs(force * m.cos(rad)), 12)
if m.degrees(rad) > 90 and m.degrees(rad) < 270:
xComponent = round(-abs(xComponent), 12)
xDict.update({xKey.format(index + 1): xComponent})
if debugFlag == True:
print("Uppdaterat " + xKey.format(index) + " med värdet: " + str(xComponent))
def yComponentSplit(index, force, rad):
yComponent = round(abs(force * m.sin(rad)), 12)
if m.degrees(rad) > 180 and m.degrees(rad) < 360:
yComponent = round(-abs(yComponent), 12)
yDict.update({yKey.format(index + 1): yComponent})
if debugFlag == True:
print("Uppdaterat " + yKey.format(index) + " med värdet: " + str(yComponent))
def method():
method = input("Välj operation: ")
if method == "" or method == "+":
sumOfForces()
def sumOfForces():
global forceSum
global xSum
global ySum
global forceSumAngle
xDictValues = list(xDict.values())
yDictValues = list(yDict.values())
xSum = m.fsum(xDictValues)
ySum = m.fsum(yDictValues)
forceSum = round(m.sqrt((xSum**2) + (ySum**2)), 12)
if xSum != 0.0 and ySum != 0.0:
forceSumAngle = m.atan(abs(ySum) / abs(xSum))
if xSum < 0 and ySum > 0:
forceSumAngle = m.radians(180) - forceSumAngle
elif xSum < 0 and ySum < 0:
forceSumAngle = m.radians(180) + forceSumAngle
elif xSum > 0 and ySum < 0:
forceSumAngle = m.radians(360) - forceSumAngle
elif xSum == 0.0 and ySum != 0.0:
if ySum > 0.0:
forceSumAngle = m.radians(90)
elif ySum < 0.0:
forceSumAngle = m.radians(270)
elif ySum == 0.0 and xSum != 0.0:
if xSum > 0.0:
forceSumAngle = m.radians(0.0)
elif xSum < 0.0:
forceSumAngle = m.radians(180.0)
def presentResults():
print("Summa x: " + str(xSum))
print("Summa y: " + str(ySum))
print("Kraft: " + str(forceSum))
print("Vinkel: " + str(m.degrees(forceSumAngle)))
print()
renderAscii("Forces")
while True:
defAndClear()
inputForces()
split()
method()
presentResults()
if input() == "b":
break