TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
ground_to_ground_projectile_motion.cpp
Go to the documentation of this file.
1
12#include <cassert>
13#define _USE_MATH_DEFINES
14#include <cmath>
15#include <iostream>
16
21namespace physics {
34double degrees_to_radians(double degrees){
35 double radians = degrees * (M_PI / 180);
36 return radians;
37}
38
46template <typename T>
47T time_of_flight(T initial_velocity, T angle, double gravity = 9.81) {
48 double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
49 return 2.0 * Viy / gravity;
50}
51
58template <typename T>
59T horizontal_range(T initial_velocity, T angle, T time) {
60 double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity
61 return Vix * time;
62}
63
71template <typename T>
72T max_height(T initial_velocity, T angle, double gravity = 9.81) {
73 double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
74 return (std::pow(Viy, 2) / (2.0 * gravity));
75}
76} // namespace ground_to_ground_projectile_motion
77} // namespace physics
78
83static void test() {
84 // initial input variables
85 double initial_velocity = 5.0; // double initial_velocity input
86 double angle = 40.0; // double angle input
87
88 // 1st test
89 double expected_time_of_flight = 0.655; // expected time output
90 double flight_time_output =
91 std::round(physics::ground_to_ground_projectile_motion::time_of_flight(initial_velocity, angle) * 1000.0) /
92 1000.0; // round output to 3 decimal places
93
94 std::cout << "Projectile Flight Time (double)" << std::endl;
95 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
96 std::cout << "Input Angle: " << angle << std::endl;
97 std::cout << "Expected Output: " << expected_time_of_flight << std::endl;
98 std::cout << "Output: " << flight_time_output << std::endl;
99 assert(flight_time_output == expected_time_of_flight);
100 std::cout << "TEST PASSED" << std::endl << std::endl;
101
102 // 2nd test
103 double expected_horizontal_range = 2.51; // expected range output
104 double horizontal_range_output =
105 std::round(physics::ground_to_ground_projectile_motion::horizontal_range(initial_velocity, angle,
106 flight_time_output) *
107 100.0) /
108 100.0; // round output to 2 decimal places
109
110 std::cout << "Projectile Horizontal Range (double)" << std::endl;
111 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
112 std::cout << "Input Angle: " << angle << std::endl;
113 std::cout << "Input Time Of Flight: " << flight_time_output << std::endl;
114 std::cout << "Expected Output: " << expected_horizontal_range << std::endl;
115 std::cout << "Output: " << horizontal_range_output << std::endl;
116 assert(horizontal_range_output == expected_horizontal_range);
117 std::cout << "TEST PASSED" << std::endl << std::endl;
118
119 // 3rd test
120 double expected_max_height = 0.526; // expected height output
121 double max_height_output =
122 std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) /
123 1000.0; // round output to 3 decimal places
124
125 std::cout << "Projectile Max Height (double)" << std::endl;
126 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
127 std::cout << "Input Angle: " << angle << std::endl;
128 std::cout << "Expected Output: " << expected_max_height << std::endl;
129 std::cout << "Output: " << max_height_output << std::endl;
130 assert(max_height_output == expected_max_height);
131 std::cout << "TEST PASSED" << std::endl << std::endl;
132}
133
138int main() {
139 test(); // run self-test implementations
140 return 0;
141}
T horizontal_range(T initial_velocity, T angle, T time)
Calculate the horizontal distance that the projectile travels.
T time_of_flight(T initial_velocity, T angle, double gravity=9.81)
Calculate the time of flight.
T max_height(T initial_velocity, T angle, double gravity=9.81)
Calculate the max height of the projectile.
static void test()
Self-test implementations.
double degrees_to_radians(double degrees)
Convert radians to degrees.
int main()
Main function.
Functions for the Ground to ground projectile motion equation.
for IO operations