Skip to content

Commit ff2a162

Browse files
authored
Add files via upload
1 parent 4951686 commit ff2a162

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""
2+
Horizontal Projectile Motion problem in physics.
3+
This algorithm solves a specific problem in which
4+
the motion starts from the ground as can be seen below:
5+
(v = 0)
6+
**
7+
* *
8+
* *
9+
* *
10+
* *
11+
* *
12+
GROUND GROUND
13+
14+
For more info: https://en.wikipedia.org/wiki/Projectile_motion
15+
"""
16+
17+
# Importing packages
18+
from math import pi, sin
19+
20+
# Acceleration Constant on hearth (unit m/s^2)
21+
g = 9.80665
22+
23+
24+
def angle_to_radians(angle):
25+
"""
26+
Convert an angle from degrees to randians
27+
"""
28+
return angle * pi / 180
29+
30+
31+
def horizontal_distance(init_velocity: float, angle: float) -> float:
32+
"""
33+
Returns the horizontal distance that the object cover
34+
35+
Formula:
36+
v_0^2 * sin(2 * alpha)
37+
---------------------
38+
g
39+
40+
v_0 - initial velocity
41+
alpha - angle
42+
43+
>>> horizontal_distance(30, 45)
44+
91.77
45+
>>> horizontal_distance(100, 78)
46+
414.76
47+
"""
48+
radians = angle_to_radians(2 * angle)
49+
return round((init_velocity ** 2) * sin(radians) / g, 2)
50+
51+
52+
def max_height(init_velocity: float, angle: float) -> float:
53+
"""
54+
Returns the maximum height that the object reach
55+
56+
Formula:
57+
v_0^2 * sin^2(alpha)
58+
--------------------
59+
2g
60+
61+
v_0 - initial velocity
62+
alpha - angle
63+
64+
>>> max_height(30, 45)
65+
22.94
66+
>>> max_height(100, 78)
67+
487.82
68+
"""
69+
70+
radians = angle_to_radians(angle)
71+
return round(((init_velocity ** 2) * (sin(radians)) ** 2) / (2 * g), 2)
72+
73+
74+
def total_time(init_velocity: float, angle: float) -> float:
75+
"""
76+
Returns total time of the motion
77+
78+
Formula:
79+
2 * v_0 * sin(alpha)
80+
--------------------
81+
g
82+
83+
v_0 - initial velocity
84+
alpha - angle
85+
86+
>>> total_time(30, 45)
87+
4.33
88+
>>> total_time(100, 78)
89+
19.95
90+
"""
91+
92+
radians = angle_to_radians(angle)
93+
return round((2 * init_velocity) * (sin(radians)) / g, 2)
94+
95+
96+
def test_motion() -> None:
97+
"""
98+
>>> test_motion()
99+
"""
100+
v0, angle = 25, 20
101+
assert 40.97 == horizontal_distance(v0, angle)
102+
assert 3.73 == max_height(v0, angle)
103+
assert 1.74 == total_time(v0, angle)
104+
105+
106+
if __name__ == "__main__":
107+
108+
# Get input from user
109+
init_vel = float(input("Initial Velocity: "))
110+
111+
# Get input from user
112+
angle = float(input("angle: "))
113+
114+
# Ensure valid angle
115+
if angle > 90 or angle <= 0:
116+
print("Error: Invalid angle. Range is 1-90 degrees.")
117+
118+
# Ensure valid velocity
119+
elif init_vel < 0:
120+
print("Error: Invalid velocity. Should be a positive number.")
121+
122+
# Print results
123+
else:
124+
print()
125+
h_dis = str(horizontal_distance(init_vel, angle))
126+
v_dis = str(max_height(init_vel, angle))
127+
t_time = str(total_time(init_vel, angle))
128+
print("Results: ")
129+
print("Horizontal Distance: " + h_dis + " [m]")
130+
print("Maximum Height: " + v_dis + " [m]")
131+
print("Total Time: " + t_time + " [s]")

0 commit comments

Comments
 (0)