TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
ground_to_ground_projectile_motion.cpp
Go to the documentation of this file.
1
11
12
#include <cassert>
13
#define _USE_MATH_DEFINES
14
#include <cmath>
15
#include <iostream>
16
21
22
// Define gravity as a constant within guidelines
23
constexpr
double
GRAVITY
= 9.80665;
24
25
26
namespace
physics
{
32
namespace
ground_to_ground_projectile_motion
{
38
39
double
degrees_to_radians
(
double
degrees){
40
double
radians = degrees * (M_PI / 180);
41
return
radians;
42
}
43
51
template
<
typename
T>
52
T
time_of_flight
(T initial_velocity, T angle,
double
gravity =
GRAVITY
) {
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
}
56
63
template
<
typename
T>
64
T
horizontal_range
(T initial_velocity, T angle, T time) {
65
double
Vix = initial_velocity * (std::cos(
degrees_to_radians
(angle)));
// calculate x component of the initial velocity
66
return
Vix * time;
67
}
68
76
template
<
typename
T>
77
T
max_height
(T initial_velocity, T angle,
double
gravity =
GRAVITY
) {
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
}
81
}
// namespace ground_to_ground_projectile_motion
82
}
// namespace physics
83
88
static
void
test
() {
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
}
138
143
int
main
() {
144
test
();
// run self-test implementations
145
return
0;
146
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
physics::ground_to_ground_projectile_motion::horizontal_range
T horizontal_range(T initial_velocity, T angle, T time)
Calculate the horizontal distance that the projectile travels.
Definition
ground_to_ground_projectile_motion.cpp:64
physics::ground_to_ground_projectile_motion::max_height
T max_height(T initial_velocity, T angle, double gravity=GRAVITY)
Calculate the max height of the projectile.
Definition
ground_to_ground_projectile_motion.cpp:77
physics::ground_to_ground_projectile_motion::time_of_flight
T time_of_flight(T initial_velocity, T angle, double gravity=GRAVITY)
Calculate the time of flight.
Definition
ground_to_ground_projectile_motion.cpp:52
GRAVITY
constexpr double GRAVITY
Standard gravity (m/s^2).
Definition
ground_to_ground_projectile_motion.cpp:23
physics::ground_to_ground_projectile_motion::degrees_to_radians
double degrees_to_radians(double degrees)
Convert radians to degrees.
Definition
ground_to_ground_projectile_motion.cpp:39
ground_to_ground_projectile_motion
Functions for the Ground to ground projectile motion equation.
physics
for IO operations
physics
ground_to_ground_projectile_motion.cpp
Generated by
1.18.0