Algorithms_in_C++ 1.0.0
Set of 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:

Namespaces

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

Functions

double physics::ground_to_ground_projectile_motion::degrees_to_radians (double radian, double PI=3.14)
 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

Function Documentation

◆ degrees_to_radians()

double physics::ground_to_ground_projectile_motion::degrees_to_radians ( double radian,
double PI = 3.14 )

Convert radians to degrees.

Parameters
radianAngle in radians
PIThe definition of the constant PI
Returns
Angle in degrees
33 {
34 return (radian * (PI / 180));
35}
Here is the call graph for this function:

◆ 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
57 {
58 double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity
59 return Vix * time;
60}
T cos(T... args)
double degrees_to_radians(double radian, double PI=3.14)
Convert radians to degrees.
Definition ground_to_ground_projectile_motion.cpp:33
T time(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
136 {
137 test(); // run self-test implementations
138 return 0;
139}
static void test()
Self-test implementations.
Definition ground_to_ground_projectile_motion.cpp:81
Here is the call graph for this function:

◆ 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
70 {
71 double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
72 return (std::pow(Viy, 2) / (2.0 * gravity));
73}
T pow(T... args)
T sin(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
81 {
82 // initial input variables
83 double initial_velocity = 5.0; // double initial_velocity input
84 double angle = 40.0; // double angle input
85
86 // 1st test
87 double expected_time_of_flight = 0.655; // expected time output
88 double flight_time_output =
89 std::round(physics::ground_to_ground_projectile_motion::time_of_flight(initial_velocity, angle) * 1000.0) /
90 1000.0; // round output to 3 decimal places
91
92 std::cout << "Projectile Flight Time (double)" << std::endl;
93 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
94 std::cout << "Input Angle: " << angle << std::endl;
95 std::cout << "Expected Output: " << expected_time_of_flight << std::endl;
96 std::cout << "Output: " << flight_time_output << std::endl;
97 assert(flight_time_output == expected_time_of_flight);
98 std::cout << "TEST PASSED" << std::endl << std::endl;
99
100 // 2nd test
101 double expected_horizontal_range = 2.51; // expected range output
102 double horizontal_range_output =
103 std::round(physics::ground_to_ground_projectile_motion::horizontal_range(initial_velocity, angle,
104 flight_time_output) *
105 100.0) /
106 100.0; // round output to 2 decimal places
107
108 std::cout << "Projectile Horizontal Range (double)" << std::endl;
109 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
110 std::cout << "Input Angle: " << angle << std::endl;
111 std::cout << "Input Time Of Flight: " << flight_time_output << std::endl;
112 std::cout << "Expected Output: " << expected_horizontal_range << std::endl;
113 std::cout << "Output: " << horizontal_range_output << std::endl;
114 assert(horizontal_range_output == expected_horizontal_range);
115 std::cout << "TEST PASSED" << std::endl << std::endl;
116
117 // 3rd test
118 double expected_max_height = 0.526; // expected height output
119 double max_height_output =
120 std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) /
121 1000.0; // round output to 3 decimal places
122
123 std::cout << "Projectile Max Height (double)" << std::endl;
124 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
125 std::cout << "Input Angle: " << angle << std::endl;
126 std::cout << "Expected Output: " << expected_max_height << std::endl;
127 std::cout << "Output: " << max_height_output << std::endl;
128 assert(max_height_output == expected_max_height);
129 std::cout << "TEST PASSED" << std::endl << std::endl;
130}
T endl(T... args)
T round(T... args)
Here is the call graph for this function:

◆ 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
45 {
46 double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
47 return 2.0 * Viy / gravity;
48}
Here is the call graph for this function: