-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectileMotion.swift
273 lines (259 loc) · 14.7 KB
/
ProjectileMotion.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import SwiftUI
import LaTeXSwiftUI
import Combine
struct ProjectileMotion: View {
//Initializes input string variables
@State private var Vi: String = ""
@State private var AofL: String = ""
@State private var h: String = ""
@State private var showPopover = false
var body: some View {
ScrollView {
VStack {
//Gives user directions on which box(es) to enter
Text("Enter values to calculate time of flight, distance, and maximum height. 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("Initial Velocity (m/s)", text: $Vi.blockingNumbersAndPunctuation())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.numbersAndPunctuation)
TextField("Angle of Launch (deg)", text: $AofL.blockingNumbersAndPunctuation())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.numbersAndPunctuation)
TextField("Launch height (m)", text: $h.blockingNumbersAndPunctuation())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.keyboardType(.numbersAndPunctuation)
}
//Conditional statements based on which text boxes are full and empty
VStack {
if (!Vi.isEmpty && !AofL.isEmpty && !h.isEmpty) {
let initialVelocity = Double(Vi) ?? 0
let angleOfLaunch = Double(AofL) ?? 0
let angleOfLaunchRad = angleOfLaunch * Double.pi / 180
let initialHeight = Double(h) ?? 0
let sinAoL: Double = sin(angleOfLaunchRad)
let horizontalVelocity: Double = initialVelocity * cos(angleOfLaunchRad)
let verticalVelocity: Double = initialVelocity * sin(angleOfLaunchRad)
let vSquared: Double = pow(verticalVelocity, 2)
let viSquared: Double = pow(initialVelocity, 2)
let sinSquared: Double = pow(sinAoL, 2)
let tOFEQ: Double = vSquared + 2 * 9.80665 * initialHeight
let timeOfFlight: Double = (verticalVelocity + sqrt(tOFEQ)) / 9.80665
let range: Double = horizontalVelocity * (verticalVelocity + (sqrt(vSquared + 2 * 9.80665 * initialHeight))) / 9.80665
let maximumHeight: Double = initialHeight + (viSquared * sinSquared / 19.6133)
ScrollView(.horizontal) {
HStack (spacing: 0){
Text("Time of flight: ")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 0))
.frame(alignment: .leading)
Text(String(timeOfFlight))
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.frame(alignment: .leading)
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(timeOfFlight)
}) {
HStack {
Image(systemName: "doc.on.doc")
Text("Copy")
} }
}
.textSelection(.enabled)
Text(" s")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.frame(maxWidth: .infinity, alignment: .leading)
}
}
ScrollView(.horizontal) {
HStack (spacing: 0){
Text("Distance: ")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 0))
.frame(alignment: .leading)
Text(String(range))
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(range)
}) {
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)
}
}
ScrollView(.horizontal) {
HStack (spacing: 0){
Text("Maximum Height: ")
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 0))
.frame(alignment: .leading)
Text(String(maximumHeight))
.fontWeight(.medium)
.multilineTextAlignment(.leading)
.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
.contextMenu {
Button(action: {
UIPasteboard.general.string = String(maximumHeight)
}) {
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("If you ever want to impress your friends with your physics knowledge, just throw something in the air and explain how the object you threw is moving along a curved path because of gravity, while its horizontal speed stays the same. You can use examples like balls, arrows, or bullets to illustrate your point. But don't actually throw bullets, that's dangerous. And don't throw arrows either, unless you have a bow. And don't throw balls at people's faces, unless they are playing dodgeball. Just be careful with what you throw, okay?")
.multilineTextAlignment(.center)
.font(.headline)
Spacer(minLength: 20)
// Formula
LaTeX("$$t = \\frac{V•sin(θ) + \\sqrt{(V•sin(θ)^2) + 2gh}}{g}$$")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
Text("Time of Flight")
.font(.headline)
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
Text("V = Initial Velocity, θ = Angle of Launch, h = Initial Height, g = Acceleration due to gravity (on Earth, 9.80665)")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
}.padding(20)
Spacer(minLength: 40)
VStack {
LaTeX("$$d = V•cos(θ) • \\frac{(V•sin(θ)) + \\sqrt{(V•sinθ)^2 + 2gh}}{g}$$")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
Text("Distance")
.font(.headline)
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
Text("d = Distance, V = Initial Velocity, θ = Angle of Launch, h = Initial Height, g = Acceleration due to gravity (on Earth, 9.80665)")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
Spacer(minLength: 40)
}.padding(20)
VStack {
LaTeX("$$H = h + \\frac{(V • sin(θ)^2}{2g}$$")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
Text("Maximum Height")
.font(.headline)
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
Text("V = Initial Velocity, θ = Angle of Launch, h = Initial Height, g = Acceleration due to gravity (on Earth, 9.80665)")
.fontWeight(.medium)
.multilineTextAlignment(.center)
.padding(7.5)
.frame(maxWidth: .infinity, alignment: .center)
Spacer(minLength: 40)
}.padding(20)
Spacer(minLength: 40)
// Example
VStack(spacing: 10) {
Text("Example")
.font(.title)
.fontWeight(.bold)
Divider()
ZStack {
// Draw the projectile
Circle()
.fill(Color.red)
.frame(width: 10, height: 10)
.padding(.bottom, 40)
// Draw the ground
Rectangle()
.fill(Color.green)
.frame(height: 1)
}
.padding(20)
VStack(alignment: .leading, spacing: 5) {
Text("Initial Velocity: 1,000,000 kg")
Text("Angle of Launch: 0.000025 m")
Text("Launch Height: 1.6334013591276333 m/s")
Text("Time of Flight: 1,000,000 kg")
Text("Distance: 0.000025 m")
Text("Maximum Height: 1.6334013591276333 m/s")
}.navigationTitle("Projectile Motion")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
showPopover = false
}) {
Image(systemName: "xmark")
}
}
}
}
}
}
}
}.navigationTitle("Projectile Motion")
}
}
struct ProjectileMotion_Previews: PreviewProvider {
static var previews: some View {
ProjectileMotion()
}
}