-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
horizontal motion code physics #4710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
a565258
Add files via upload
avivfaraj 4951686
Changed print to f-string
avivfaraj ff2a162
Add files via upload
avivfaraj 6550183
Fixes: #4710 provided return type
avivfaraj 552546f
File exists in another pull request
avivfaraj bc31e3f
imported radians from math
avivfaraj 974e0a6
Updated file according to pre-commit test
avivfaraj 1d4fc81
Updated file
avivfaraj 7bd84b4
Updated gamma
avivfaraj 2841c11
Deleted duplicate file
avivfaraj cea2deb
removed pi
avivfaraj db7db3b
reversed tests
avivfaraj 132e495
Fixed angle condition
avivfaraj c0e5071
Modified prints to f-string
avivfaraj 894fa7f
Update horizontal_projectile_motion.py
avivfaraj ee49ce7
Merge branch 'TheAlgorithms:master' into master
avivfaraj 07646ac
Update horizontal_projectile_motion.py
avivfaraj 4e2fcaf
Fixes #4710 added exceptions and tests
avivfaraj dcacc95
Added float tests
avivfaraj 3f2b238
Fixed type annotations
avivfaraj e3678fd
Fixed last annotation
avivfaraj c37bb95
Fixed annotations
avivfaraj 5b0249a
fixed format
avivfaraj ec790c6
Revert "fixed format"
avivfaraj e865929
Revert "Fixed annotations"
avivfaraj 82b4e0d
Revert "Fixed last annotation"
avivfaraj 63c7c6a
Revert "Fixed type annotations"
avivfaraj 0527866
Merge branch 'TheAlgorithms:master' into master
avivfaraj 805050e
Revert to 4e2fcaf6fb
avivfaraj 5f3486c
Fixing errors found during pre-commit
avivfaraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
""" | ||
Horizontal Projectile Motion problem in physics. | ||
This algorithm solves a specific problem in which | ||
the motion starts from the ground as can be seen below: | ||
(v = 0) | ||
** | ||
* * | ||
* * | ||
* * | ||
* * | ||
* * | ||
GROUND GROUND | ||
|
||
For more info: https://en.wikipedia.org/wiki/Projectile_motion | ||
""" | ||
|
||
# Importing packages | ||
from math import pi, sin | ||
|
||
# Acceleration Constant on hearth (unit m/s^2) | ||
g = 9.80665 | ||
|
||
|
||
def angle_to_radians(angle : float) -> float: | ||
""" | ||
Convert an angle from degrees to randians | ||
""" | ||
return angle * pi / 180 | ||
|
||
|
||
def horizontal_distance(init_velocity: float, angle: float) -> float: | ||
""" | ||
Returns the horizontal distance that the object cover | ||
|
||
Formula: | ||
v_0^2 * sin(2 * alpha) | ||
--------------------- | ||
g | ||
|
||
v_0 - initial velocity | ||
alpha - angle | ||
|
||
>>> horizontal_distance(30, 45) | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
91.77 | ||
>>> horizontal_distance(100, 78) | ||
414.76 | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
radians = angle_to_radians(2 * angle) | ||
return round((init_velocity ** 2) * sin(radians) / g, 2) | ||
|
||
|
||
def max_height(init_velocity: float, angle: float) -> float: | ||
""" | ||
Returns the maximum height that the object reach | ||
|
||
Formula: | ||
v_0^2 * sin^2(alpha) | ||
-------------------- | ||
2g | ||
|
||
v_0 - initial velocity | ||
alpha - angle | ||
|
||
>>> max_height(30, 45) | ||
22.94 | ||
>>> max_height(100, 78) | ||
487.82 | ||
""" | ||
|
||
radians = angle_to_radians(angle) | ||
return round(((init_velocity ** 2) * (sin(radians)) ** 2) / (2 * g), 2) | ||
|
||
|
||
def total_time(init_velocity: float, angle: float) -> float: | ||
""" | ||
Returns total time of the motion | ||
|
||
Formula: | ||
2 * v_0 * sin(alpha) | ||
-------------------- | ||
g | ||
|
||
v_0 - initial velocity | ||
alpha - angle | ||
|
||
>>> total_time(30, 45) | ||
4.33 | ||
>>> total_time(100, 78) | ||
19.95 | ||
""" | ||
|
||
radians = angle_to_radians(angle) | ||
return round((2 * init_velocity) * (sin(radians)) / g, 2) | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_motion() -> None: | ||
""" | ||
>>> test_motion() | ||
""" | ||
v0, angle = 25, 20 | ||
assert 40.97 == horizontal_distance(v0, angle) | ||
assert 3.73 == max_height(v0, angle) | ||
assert 1.74 == total_time(v0, angle) | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
|
||
# Get input from user | ||
init_vel = float(input("Initial Velocity: ")) | ||
|
||
# Get input from user | ||
angle = float(input("angle: ")) | ||
|
||
# Ensure valid angle | ||
if angle > 90 or angle <= 0: | ||
print("Error: Invalid angle. Range is 1-90 degrees.") | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Ensure valid velocity | ||
elif init_vel < 0: | ||
print("Error: Invalid velocity. Should be a positive number.") | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Print results | ||
else: | ||
print() | ||
h_dis = str(horizontal_distance(init_vel, angle)) | ||
v_dis = str(max_height(init_vel, angle)) | ||
t_time = str(total_time(init_vel, angle)) | ||
print("Results: ") | ||
print("Horizontal Distance: " + h_dis + " [m]") | ||
print("Maximum Height: " + v_dis + " [m]") | ||
print("Total Time: " + t_time + " [s]") | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.