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=GRAVITY)
 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=GRAVITY)
 Calculate the max height of the projectile.
static void test ()
 Self-test implementations.
int main ()
 Main function.

Variables

constexpr double GRAVITY = 9.80665
 Standard gravity (m/s^2)

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 39 of file ground_to_ground_projectile_motion.cpp.

39 {
40 double radians = degrees * (M_PI / 180);
41 return radians;
42}

◆ 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 64 of file ground_to_ground_projectile_motion.cpp.

64 {
65 double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity
66 return Vix * time;
67}
double degrees_to_radians(double degrees)
Convert radians to degrees.

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 143 of file ground_to_ground_projectile_motion.cpp.

143 {
144 test(); // run self-test implementations
145 return 0;
146}
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 = GRAVITY )

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 77 of file ground_to_ground_projectile_motion.cpp.

77 {
78 double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
79 return (std::pow(Viy, 2) / (2.0 * gravity));
80}

◆ test()

void test ( )
static

Self-test implementations.

Returns
void

Definition at line 88 of file ground_to_ground_projectile_motion.cpp.

88 {
89 // initial input variables
90 double initial_velocity = 5.0; // double initial_velocity input
91 double angle = 40.0; // double angle input
92
93 // 1st test
94 double expected_time_of_flight = 0.655; // expected time output
95 double flight_time_output =
96 std::round(physics::ground_to_ground_projectile_motion::time_of_flight(initial_velocity, angle) * 1000.0) /
97 1000.0; // round output to 3 decimal places
98
99 std::cout << "Projectile Flight Time (double)" << std::endl;
100 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
101 std::cout << "Input Angle: " << angle << std::endl;
102 std::cout << "Expected Output: " << expected_time_of_flight << std::endl;
103 std::cout << "Output: " << flight_time_output << std::endl;
104 assert(flight_time_output == expected_time_of_flight);
105 std::cout << "TEST PASSED" << std::endl << std::endl;
106
107 // 2nd test
108 double expected_horizontal_range = 2.51; // expected range output
109 double horizontal_range_output =
110 std::round(physics::ground_to_ground_projectile_motion::horizontal_range(initial_velocity, angle,
111 flight_time_output) *
112 100.0) /
113 100.0; // round output to 2 decimal places
114
115 std::cout << "Projectile Horizontal Range (double)" << std::endl;
116 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
117 std::cout << "Input Angle: " << angle << std::endl;
118 std::cout << "Input Time Of Flight: " << flight_time_output << std::endl;
119 std::cout << "Expected Output: " << expected_horizontal_range << std::endl;
120 std::cout << "Output: " << horizontal_range_output << std::endl;
121 assert(horizontal_range_output == expected_horizontal_range);
122 std::cout << "TEST PASSED" << std::endl << std::endl;
123
124 // 3rd test
125 double expected_max_height = 0.526; // expected height output
126 double max_height_output =
127 std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) /
128 1000.0; // round output to 3 decimal places
129
130 std::cout << "Projectile Max Height (double)" << std::endl;
131 std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
132 std::cout << "Input Angle: " << angle << std::endl;
133 std::cout << "Expected Output: " << expected_max_height << std::endl;
134 std::cout << "Output: " << max_height_output << std::endl;
135 assert(max_height_output == expected_max_height);
136 std::cout << "TEST PASSED" << std::endl << std::endl;
137}
T horizontal_range(T initial_velocity, T angle, T time)
Calculate the horizontal distance that the projectile travels.
T max_height(T initial_velocity, T angle, double gravity=GRAVITY)
Calculate the max height of the projectile.
T time_of_flight(T initial_velocity, T angle, double gravity=GRAVITY)
Calculate the time of flight.

◆ time_of_flight()

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

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 52 of file ground_to_ground_projectile_motion.cpp.

52 {
53 double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
54 return 2.0 * Viy / gravity;
55}

Variable Documentation

◆ GRAVITY

double GRAVITY = 9.80665
constexpr

Standard gravity (m/s^2)

Definition at line 23 of file ground_to_ground_projectile_motion.cpp.