TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
ground_to_ground_projectile_motion.cpp File Reference

Ground to ground projectile motion equation implementations. More...

#include <cassert>
#include <cmath>
#include <iostream>
Include dependency graph for ground_to_ground_projectile_motion.cpp:

Go to the source code of this file.

Namespaces

namespace  physics
 for IO operations
 
namespace  ground_to_ground_projectile_motion
 Functions for the Ground to ground projectile motion equation.
 

Macros

#define _USE_MATH_DEFINES
 for assert()
 

Functions

double physics::ground_to_ground_projectile_motion::degrees_to_radians (double degrees)
 Convert radians to degrees.
 
template<typename T >
physics::ground_to_ground_projectile_motion::time_of_flight (T initial_velocity, T angle, double gravity=9.81)
 Calculate the time of flight.
 
template<typename T >
physics::ground_to_ground_projectile_motion::horizontal_range (T initial_velocity, T angle, T time)
 Calculate the horizontal distance that the projectile travels.
 
template<typename T >
physics::ground_to_ground_projectile_motion::max_height (T initial_velocity, T angle, double gravity=9.81)
 Calculate the max height of the projectile.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Ground to ground projectile motion equation implementations.

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
Focusucof

Definition in file ground_to_ground_projectile_motion.cpp.

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

for assert()

Definition at line 13 of file ground_to_ground_projectile_motion.cpp.

Function Documentation

◆ degrees_to_radians()

double physics::ground_to_ground_projectile_motion::degrees_to_radians ( double degrees)

Convert radians to degrees.

Parameters
radianAngle in radians
Returns
Angle in degrees

Definition at line 34 of file ground_to_ground_projectile_motion.cpp.

34 {
35 double radians = degrees * (M_PI / 180);
36 return radians;
37}

◆ horizontal_range()

template<typename T >
T physics::ground_to_ground_projectile_motion::horizontal_range ( T initial_velocity,
T angle,
T time )

Calculate the horizontal distance that the projectile travels.

Parameters
initial_velocityThe starting velocity of the projectile
timeThe time that the projectile is in the air
Returns
Horizontal distance that the projectile travels

Definition at line 59 of file ground_to_ground_projectile_motion.cpp.

59 {
60 double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity
61 return Vix * time;
62}
double degrees_to_radians(double degrees)
Convert radians to degrees.

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 138 of file ground_to_ground_projectile_motion.cpp.

138 {
139 test(); // run self-test implementations
140 return 0;
141}
static void test()
Self-test implementations.

◆ max_height()

template<typename T >
T physics::ground_to_ground_projectile_motion::max_height ( T initial_velocity,
T angle,
double gravity = 9.81 )

Calculate the max height of the projectile.

Parameters
initial_velocityThe starting velocity of the projectile
angleThe angle that the projectile is launched at in degrees
gravityThe value used for the gravity constant
Returns
The max height that the projectile reaches

Definition at line 72 of file ground_to_ground_projectile_motion.cpp.

72 {
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}

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 83 of file ground_to_ground_projectile_motion.cpp.

83 {
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}

◆ time_of_flight()

template<typename T >
T physics::ground_to_ground_projectile_motion::time_of_flight ( T initial_velocity,
T angle,
double gravity = 9.81 )

Calculate the time of flight.

Parameters
initial_velocityThe starting velocity of the projectile
angleThe angle that the projectile is launched at in degrees
gravityThe value used for the gravity constant
Returns
The time that the projectile is in the air for

Definition at line 47 of file ground_to_ground_projectile_motion.cpp.

47 {
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}