-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelativityCalculator.py
More file actions
158 lines (126 loc) · 5.58 KB
/
relativityCalculator.py
File metadata and controls
158 lines (126 loc) · 5.58 KB
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
#Jesse A. Jones
#Version: 2024-01-14.93
from tkinter import *
import math
import metricTimeToStandard
import dateHandling
import decimal
from decimal import Decimal
decimal.getcontext().prec = 1000
#Takes in a distance and velocity
# (optionally in light years and light speed percentage)
# and calculates the time it takes relative to the traveler to get there.
class RelCalc(object):
def __init__(self, window = None):
self.window = window
#Holds quit button.
self.frameTop = Frame(self.window)
self.frameTop.pack(side = TOP)
#Quits program when pressed.
self.quitButton = Button(self.frameTop, text = "Quit",
font = "Ariel 20", command = self.quitButtonAction)
self.quitButton.pack()
#Holds velocity and distance input fields,
# calculation button, and time output.
self.frameBottom = Frame(self.window)
self.frameBottom.pack(side = BOTTOM)
#Distance input field.
self.messageII = Label(self.frameBottom, text = "Enter Distance (m) ", font = "Ariel 20")
self.messageII.grid(row = 0, column = 0)
self.distance = Entry(self.frameBottom, font = "Ariel 20")
self.distance.grid(row = 1, column = 0)
#Velocity input field.
self.messageI = Label(self.frameBottom, text = "Enter Velocity (m/s)", font = "Ariel 20")
self.messageI.grid(row = 2, column = 0)
self.velocity = Entry(self.frameBottom, font = "Ariel 20")
self.velocity.grid(row = 3, column = 0)
#Holds second count.
self.messageTime = Label(self.frameBottom, text = "", font = "Ariel 20", anchor = "w")
self.messageTime.grid(row = 5, column = 0)
#Displays second unit.
self.messageTimeSeg = Label(self.frameBottom, text = "", font = "Ariel 20", anchor = "w")
self.messageTimeSeg.grid(row = 6, column = 0)
#Displays seconds converted to a more standard time duration.
self.messageTimeSegOther = Label(self.frameBottom, text = "", font = "Ariel 20", anchor = "w")
self.messageTimeSegOther.grid(row = 7, column = 0)
#Calculates time to travel distance and velocity when pressed.
self.convButton = Button(self.frameBottom, text = "Find Time",
font = "Ariel 20", command = self.timeFind)
self.convButton.grid(row = 4, column = 0)
self.convToStand = metricTimeToStandard.MetricToStandard()
self.C: Decimal = Decimal(299792458)
self.parse = dateHandling.GetDate()
#Quits program when called.
def quitButtonAction(self):
self.window.destroy()
#Determines time taken and displays result in seconds and standard time.
def timeFind(self):
#Calculates time in seconds and displays result.
time = self.calcRelative()
self.messageTime["text"] = float(time)
self.messageTimeSeg["text"] = "Seconds"
#Displays time as infinite or a finite amount of converted time.
if (time == math.inf):
self.messageTimeSegOther["text"] = "Inf yr"
else:
self.messageTimeSegOther["text"] = self.convToStand.metricToStandard((time / 86400), True)
#Fetches speed and distance from inputs
# and sets class variables unnescesarily.
def distAndSpeedGet(self) -> list[Decimal]:
#Parses the input velocity.
vel = self.velocity.get()
try:
#Accounts for if light speed percentage is given.
#Otherwise just converts to regular decimal.
if "%" in vel:
vel = vel.replace("%", "")
vel = Decimal(vel) / Decimal("100")
vel = vel * self.C
else:
vel = Decimal(vel)
except decimal.InvalidOperation:
vel = Decimal(0)
#Parses input distance.
dist = self.distance.get()
try:
#Accounts for if light year is given as distance unit.
#If not given, meters are assumed.
if "ly" in dist:
dist = dist.replace("ly", "")
dist = Decimal(dist) * Decimal(9.461e+15)
else:
dist = Decimal(dist)
except decimal.InvalidOperation:
dist = Decimal(0)
return [dist, vel]
#Calculates time distortion based on velocity and speed.
def calcTime(self) -> list[Decimal]:
retLs = self.distAndSpeedGet()
d: Decimal = retLs[0]
v: Decimal = retLs[1]
#If velocity is equal to or larger than C, distortion is infinite.
if v >= self.C:
return [math.inf, d, v]
else:
#This calculation is based on Einstein's relativisitic equations.
distortion = (Decimal(1) / Decimal(math.sqrt(Decimal(1) - ((v) ** Decimal(2)) / ((self.C) ** Decimal(2)))))
return [distortion, d, v]
#Calculates time with reletavistic distortion.
def calcRelative(self) -> Decimal:
retLs = self.calcTime()
distort = retLs[0]
d = retLs[1]
v = retLs[2]
#Time is infinity if movement never occurs.
if (v == 0):
return math.inf
#Calculates time with distortion and returns it.
newtonTime = d / abs(v)
return newtonTime / distort
def main():
root = Tk()
root.title("Relativity Calculator (Assuming Constant Velocity)")
dateAndTime = RelCalc(root)
root.mainloop()
if __name__ == "__main__":
main()