-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrbitalVelocity.swift
241 lines (226 loc) · 12.4 KB
/
OrbitalVelocity.swift
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import SwiftUI
import LaTeXSwiftUI
import Combine
struct OrbitalVelocity: View {
//Initializes input string variables
@State private var v: String = ""
@State private var m: String = ""
@State private var r: String = ""
@State private var showPopover = false
var body: some View {
ScrollView {
VStack {
//Gives user directions on which boxes to enter
Text("Enter known values. Leave unknown values empty. Calculation automatically populates! To copy your result, simply long-press. Click on i-button to learn more!")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(20.0)
.frame(maxWidth: .infinity, alignment: .leading)
//Allows user to input the values
TextField("Velocity (m/s)", text: $v.blockingNumbersAndPunctuation())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.numbersAndPunctuation)
TextField("Mass (kg)", text: $m.blockingNumbersAndPunctuation())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.numbersAndPunctuation)
TextField("Radius (m) ", text: $r.blockingNumbersAndPunctuation())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.numbersAndPunctuation)
}
//Converts the string values into numerical values
let velocity: Double = Double(v) ?? 0
let mass: Double = Double(m) ?? 0
let radius: Double = Double(r) ?? 0
let G = 6.67e-11
//Conditional statements based on which text boxes are full and empty
VStack {
if (v.isEmpty && !m.isEmpty && !r.isEmpty) {
let velocity: Double = sqrt((G * mass)/radius)
ScrollView(.horizontal) {
HStack (spacing: 0){
Text("Velocity: ")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 0))
.frame(alignment: .leading)
Text(String(velocity))
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.frame(alignment: .leading)
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(mass)
}) {
HStack {
Image(systemName: "doc.on.doc")
Text("Copy")
} }
}
.textSelection(.enabled)
Text(" m/s")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
else if (!v.isEmpty && m.isEmpty && !r.isEmpty) {
let mass: Double = (pow(velocity, 2) * radius) / G
ScrollView(.horizontal) {
HStack (spacing: 0){
Text("Mass: ")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 0))
.frame(alignment: .leading)
Text(String(mass))
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(mass)
}) {
HStack {
Image(systemName: "doc.on.doc")
Text("Copy")
} }
}
.frame(alignment: .leading)
.textSelection(.enabled)
Text(" kg")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(mass)
}) {
HStack {
Image(systemName: "doc.on.doc")
Text("Copy")
} }
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
else if (!v.isEmpty && !m.isEmpty && r.isEmpty) {
let radius: Double = (mass * G) / (pow(velocity, 2))
ScrollView(.horizontal) {
HStack (spacing: 0){
Text("Radius: ")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 0))
.frame(alignment: .leading)
Text(String(radius))
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(radius)
}) {
HStack {
Image(systemName: "doc.on.doc")
Text("Copy")
} }
}
.frame(alignment: .leading)
.textSelection(.enabled)
Text(" m")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
}
//Info-circle popup
.toolbar {
Button(action: {
showPopover = true
}) {
Image(systemName: "info.circle")
}
}
//Popover sheet view
.sheet(isPresented: $showPopover) {
NavigationView {
ScrollView {
VStack {
Spacer(minLength: 20)
// Description
Text("Orbital velocity is not just a fancy term for how fast you can spin around in circles. It's actually a very important concept in physics and astronomy. Orbital velocity is the speed at which an object needs to move in order to orbit around another object. For example, the Earth orbits around the Sun at an average speed of about 30 kilometers per second. That's pretty fast, right? But if the Earth moved any slower, it would fall into the Sun and burn up. And if it moved any faster, it would escape the Sun's gravity and fly away into space.")
.multilineTextAlignment(.center)
.padding(20)
.font(.headline)
Spacer(minLength: 20)
// Formula
LaTeX("$$v = \\sqrt{\\frac{GM}{r}}$$")
Text("v = Velocity, G = Gravitational Constant, M = mass, r = radius")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
SubSuperScriptText(inputString: "Gravitational Constant = 6.67 • 10^{-11}", bodyFont: .callout, subScriptFont: .caption, baseLine: 6.0)
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
Spacer(minLength: 40)
// Example
VStack(spacing: 10) {
Text("Example")
.font(.title)
.fontWeight(.bold)
Divider()
HStack(spacing: 30) {
// Box representing the object
Circle()
.fill(
RadialGradient(
gradient: Gradient(colors: [.blue, .gray]),
center: .center,
startRadius: 0,
endRadius: 50
)
)
.frame(width: 50, height: 50)
// Labels showing the object's properties
VStack(alignment: .leading, spacing: 5) {
Text("Mass: 1,000,000 kg")
Text("Radius: 0.000025 m")
Text("Velocity: 1.6334013591276333 m/s")
}.navigationTitle("Orbital Velocity")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showPopover = false
}) {
Image(systemName: "xmark")
}
}
}
}
}
}
}
}
}
}
.navigationTitle("Orbital Velocity")
}
}
struct OrbitalVelocity_Previews: PreviewProvider {
static var previews: some View {
OrbitalVelocity()
}
}