Skip to content

Commit 4561ebd

Browse files
author
Priyanshu1303d
committed
[FEAT] Add general purpose Projectile Motion algorithm
1 parent ab65ac6 commit 4561ebd

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.thealgorithms.physics;
2+
3+
/**
4+
*
5+
* This implementation calculates the flight path of a projectile launched from any INITIAL HEIGHT.
6+
* It is a more flexible version of the ground-to-ground model.
7+
*
8+
* @see <a href="https://en.wikipedia.org/wiki/Projectile_motion">Wikipedia - Projectile Motion</a>
9+
* @author [Priyanshu Kumar Singh](https://github.com/Priyanshu1303d)
10+
*/
11+
public final class ProjectileMotion {
12+
13+
private ProjectileMotion() {
14+
}
15+
16+
/** Standard Earth gravity constant*/
17+
private static final double GRAVITY = 9.80665;
18+
19+
/**
20+
* A simple container for the results of a projectile motion calculation.
21+
*/
22+
public static final class Result {
23+
private final double timeOfFlight;
24+
private final double horizontalRange;
25+
private final double maxHeight;
26+
27+
public Result(double timeOfFlight, double horizontalRange, double maxHeight) {
28+
this.timeOfFlight = timeOfFlight;
29+
this.horizontalRange = horizontalRange;
30+
this.maxHeight = maxHeight;
31+
}
32+
33+
/** @return The total time the projectile is in the air (seconds). */
34+
public double getTimeOfFlight() {
35+
return timeOfFlight;
36+
}
37+
38+
/** @return The total horizontal distance traveled (meters). */
39+
public double getHorizontalRange() {
40+
return horizontalRange;
41+
}
42+
43+
/** @return The maximum vertical height from the ground (meters). */
44+
public double getMaxHeight() {
45+
return maxHeight;
46+
}
47+
}
48+
49+
/**
50+
* Calculates projectile trajectory using standard Earth gravity.
51+
*
52+
* @param initialVelocity Initial speed of the projectile (m/s).
53+
* @param launchAngleDegrees Launch angle from the horizontal (degrees).
54+
* @param initialHeight Starting height of the projectile (m).
55+
* @return A {@link Result} object with the trajectory data.
56+
*/
57+
public static Result calculateTrajectory(double initialVelocity, double launchAngleDegrees, double initialHeight) {
58+
return calculateTrajectory(initialVelocity, launchAngleDegrees, initialHeight, GRAVITY);
59+
}
60+
61+
/**
62+
* Calculates projectile trajectory with a custom gravity value.
63+
*
64+
* @param initialVelocity Initial speed (m/s). Must be non-negative.
65+
* @param launchAngleDegrees Launch angle (degrees).
66+
* @param initialHeight Starting height (m). Must be non-negative.
67+
* @param gravity Acceleration due to gravity (m/s^2). Must be positive.
68+
* @return A {@link Result} object with the trajectory data.
69+
*/
70+
public static Result calculateTrajectory(double initialVelocity, double launchAngleDegrees, double initialHeight, double gravity) {
71+
if (initialVelocity < 0 || initialHeight < 0 || gravity <= 0) {
72+
throw new IllegalArgumentException("Velocity, height, and gravity must be non-negative, and gravity must be positive.");
73+
}
74+
75+
double launchAngleRadians = Math.toRadians(launchAngleDegrees);
76+
double initialVerticalVelocity = initialVelocity * Math.sin(launchAngleRadians); // Initial vertical velocity
77+
double initialHorizontalVelocity = initialVelocity * Math.cos(launchAngleRadians); // Initial horizontal velocity
78+
79+
// Correctly calculate total time of flight using the quadratic formula for vertical motion.
80+
// y(t) = y0 + initialVerticalVelocity*t - 0.5*g*t^2. We solve for t when y(t) = 0.
81+
double totalTimeOfFlight = (initialVerticalVelocity + Math.sqrt(initialVerticalVelocity * initialVerticalVelocity + 2 * gravity * initialHeight)) / gravity;
82+
83+
// Calculate max height. If launched downwards, max height is the initial height.
84+
double maxHeight;
85+
if (initialVerticalVelocity > 0) {
86+
double heightGained = (initialVerticalVelocity * initialVerticalVelocity) / (2 * gravity);
87+
maxHeight = initialHeight + heightGained;
88+
} else {
89+
maxHeight = initialHeight;
90+
}
91+
92+
double horizontalRange = initialHorizontalVelocity * totalTimeOfFlight;
93+
94+
return new Result(totalTimeOfFlight, horizontalRange, maxHeight);
95+
}
96+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.physics;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Test class for the general-purpose ProjectileMotion calculator.
11+
*
12+
*/
13+
final class ProjectileMotionTest {
14+
15+
private static final double DELTA = 1e-4; // Tolerance for comparing double values
16+
17+
@Test
18+
@DisplayName("Test ground-to-ground launch (initial height is zero)")
19+
void testGroundToGroundLaunch() {
20+
ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(50, 30, 0);
21+
assertEquals(5.0986, result.getTimeOfFlight(), DELTA);
22+
assertEquals(220.7750, result.getHorizontalRange(), DELTA);
23+
assertEquals(31.8661, result.getMaxHeight(), DELTA);
24+
}
25+
26+
@Test
27+
@DisplayName("Test launch from an elevated position")
28+
void testElevatedLaunch() {
29+
ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(30, 45, 100);
30+
assertEquals(7.1705, result.getTimeOfFlight(), DELTA);
31+
assertEquals(152.1091, result.getHorizontalRange(), DELTA);
32+
assertEquals(122.9436, result.getMaxHeight(), DELTA); // Final corrected value
33+
}
34+
35+
@Test
36+
@DisplayName("Test launch straight up (90 degrees)")
37+
void testVerticalLaunch() {
38+
ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(40, 90, 20);
39+
assertEquals(8.6303, result.getTimeOfFlight(), DELTA);
40+
assertEquals(0.0, result.getHorizontalRange(), DELTA);
41+
assertEquals(101.5773, result.getMaxHeight(), DELTA);
42+
}
43+
44+
@Test
45+
@DisplayName("Test horizontal launch from a height (0 degrees)")
46+
void testHorizontalLaunch() {
47+
ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(25, 0, 80);
48+
assertEquals(4.0392, result.getTimeOfFlight(), DELTA);
49+
assertEquals(100.9809, result.getHorizontalRange(), DELTA);
50+
assertEquals(80.0, result.getMaxHeight(), DELTA);
51+
}
52+
53+
@Test
54+
@DisplayName("Test downward launch from a height (negative angle)")
55+
void testDownwardLaunchFromHeight() {
56+
ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(20, -30, 100);
57+
assertEquals(3.6100, result.getTimeOfFlight(), DELTA);
58+
assertEquals(62.5268, result.getHorizontalRange(), DELTA);
59+
assertEquals(100.0, result.getMaxHeight(), DELTA);
60+
}
61+
62+
@Test
63+
@DisplayName("Test invalid arguments throw an exception")
64+
void testInvalidInputs() {
65+
assertThrows(IllegalArgumentException.class, () -> ProjectileMotion.calculateTrajectory(-10, 45, 100));
66+
assertThrows(IllegalArgumentException.class, () -> ProjectileMotion.calculateTrajectory(10, 45, -100));
67+
assertThrows(IllegalArgumentException.class, () -> ProjectileMotion.calculateTrajectory(10, 45, 100, 0));
68+
}
69+
}

0 commit comments

Comments
 (0)