amkj84 commited on
Commit
119ca51
·
verified ·
1 Parent(s): fa510fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ import time
5
+
6
+ # Page Configurations
7
+ st.set_page_config(
8
+ page_title="Gun Firing Simulation",
9
+ layout="centered"
10
+ )
11
+
12
+ # Title and Description
13
+ st.title("Gun Firing Simulation")
14
+ st.write("This app simulates the firing of a bullet from a gun. Watch the bullet travel along its trajectory!")
15
+
16
+ # Parameters Input
17
+ st.sidebar.header("Simulation Parameters")
18
+ initial_velocity = st.sidebar.number_input("Initial Velocity (m/s):", min_value=1, max_value=1000, value=500, step=10)
19
+ angle = st.sidebar.slider("Firing Angle (degrees):", min_value=0, max_value=90, value=45)
20
+ gravity = st.sidebar.number_input("Gravity (m/s²):", min_value=1.0, max_value=20.0, value=9.8, step=0.1)
21
+
22
+ def simulate_bullet():
23
+ # Constants
24
+ angle_rad = np.radians(angle)
25
+
26
+ # Time of flight
27
+ time_of_flight = (2 * initial_velocity * np.sin(angle_rad)) / gravity
28
+
29
+ # Maximum range
30
+ max_range = (initial_velocity**2 * np.sin(2 * angle_rad)) / gravity
31
+
32
+ # Maximum height
33
+ max_height = (initial_velocity**2 * (np.sin(angle_rad)**2)) / (2 * gravity)
34
+
35
+ # Time intervals
36
+ t = np.linspace(0, time_of_flight, num=500)
37
+
38
+ # Trajectory equations
39
+ x = initial_velocity * np.cos(angle_rad) * t
40
+ y = initial_velocity * np.sin(angle_rad) * t - 0.5 * gravity * t**2
41
+
42
+ return x, y, max_range, max_height, time_of_flight
43
+
44
+ # Simulate and Plot
45
+ if st.button("Fire Bullet!"):
46
+ st.write("## Bullet Trajectory")
47
+ st.write("Simulating... Hold on!")
48
+
49
+ x, y, max_range, max_height, time_of_flight = simulate_bullet()
50
+
51
+ # Plot
52
+ fig, ax = plt.subplots()
53
+ ax.plot(x, y, label="Bullet Trajectory")
54
+ ax.axhline(0, color='black', linewidth=0.5, linestyle='--')
55
+ ax.set_xlabel("Distance (m)")
56
+ ax.set_ylabel("Height (m)")
57
+ ax.set_title("Bullet Trajectory Simulation")
58
+ ax.legend()
59
+
60
+ st.pyplot(fig)
61
+
62
+ # Display parameters
63
+ st.write(f"**Maximum Range:** {max_range:.2f} meters")
64
+ st.write(f"**Maximum Height:** {max_height:.2f} meters")
65
+ st.write(f"**Time of Flight:** {time_of_flight:.2f} seconds")
66
+
67
+ # Animation Simulation
68
+ st.write("### Animation of the Bullet")
69
+ progress_bar = st.progress(0)
70
+ bullet_fig, bullet_ax = plt.subplots()
71
+
72
+ for i in range(len(x)):
73
+ if y[i] < 0: # Stop animation if bullet hits the ground
74
+ break
75
+ bullet_ax.clear()
76
+ bullet_ax.plot(x, y, label="Bullet Trajectory")
77
+ bullet_ax.plot(x[i], y[i], 'ro', label="Bullet Position")
78
+ bullet_ax.axhline(0, color='black', linewidth=0.5, linestyle='--')
79
+ bullet_ax.set_xlim(0, max(x))
80
+ bullet_ax.set_ylim(0, max(y) + 5)
81
+ bullet_ax.set_xlabel("Distance (m)")
82
+ bullet_ax.set_ylabel("Height (m)")
83
+ bullet_ax.set_title("Bullet Animation")
84
+ bullet_ax.legend()
85
+ st.pyplot(bullet_fig)
86
+ progress_bar.progress(int((i / len(x)) * 100))
87
+ time.sleep(0.01)
88
+
89
+ st.write("Simulation Complete!")