forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroundToGroundProjectileMotion.java
More file actions
90 lines (81 loc) · 3.36 KB
/
GroundToGroundProjectileMotion.java
File metadata and controls
90 lines (81 loc) · 3.36 KB
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
package com.thealgorithms.physics;
/**
* Ground to ground projectile motion calculator
*
* Ground to ground projectile motion is when a projectile's trajectory
* starts at the ground, reaches the apex, then falls back on the ground.
*
* @author [Yash Rajput](https://github.com/the-yash-rajput)
*/
public final class GroundToGroundProjectileMotion {
private GroundToGroundProjectileMotion() {
throw new AssertionError("No instances.");
}
/** Standard gravity constant (m/s^2) */
private static final double GRAVITY = 9.80665;
/**
* Convert degrees to radians
*
* @param degrees Angle in degrees
* @return Angle in radians
*/
private static double degreesToRadians(double degrees) {
return degrees * (Math.PI / 180.0);
}
/**
* Calculate the time of flight
*
* @param initialVelocity The starting velocity of the projectile (m/s)
* @param angle The angle that the projectile is launched at in degrees
* @return The time that the projectile is in the air for (seconds)
*/
public static double timeOfFlight(double initialVelocity, double angle) {
return timeOfFlight(initialVelocity, angle, GRAVITY);
}
/**
* Calculate the time of flight with custom gravity
*
* @param initialVelocity The starting velocity of the projectile (m/s)
* @param angle The angle that the projectile is launched at in degrees
* @param gravity The value used for the gravity constant (m/s^2)
* @return The time that the projectile is in the air for (seconds)
*/
public static double timeOfFlight(double initialVelocity, double angle, double gravity) {
double viy = initialVelocity * Math.sin(degreesToRadians(angle));
return 2.0 * viy / gravity;
}
/**
* Calculate the horizontal distance that the projectile travels
*
* @param initialVelocity The starting velocity of the projectile (m/s)
* @param angle The angle that the projectile is launched at in degrees
* @param time The time that the projectile is in the air (seconds)
* @return Horizontal distance that the projectile travels (meters)
*/
public static double horizontalRange(double initialVelocity, double angle, double time) {
double vix = initialVelocity * Math.cos(degreesToRadians(angle));
return vix * time;
}
/**
* Calculate the max height of the projectile
*
* @param initialVelocity The starting velocity of the projectile (m/s)
* @param angle The angle that the projectile is launched at in degrees
* @return The max height that the projectile reaches (meters)
*/
public static double maxHeight(double initialVelocity, double angle) {
return maxHeight(initialVelocity, angle, GRAVITY);
}
/**
* Calculate the max height of the projectile with custom gravity
*
* @param initialVelocity The starting velocity of the projectile (m/s)
* @param angle The angle that the projectile is launched at in degrees
* @param gravity The value used for the gravity constant (m/s^2)
* @return The max height that the projectile reaches (meters)
*/
public static double maxHeight(double initialVelocity, double angle, double gravity) {
double viy = initialVelocity * Math.sin(degreesToRadians(angle));
return Math.pow(viy, 2) / (2.0 * gravity);
}
}