mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-09 15:43:48 +08:00
* chore: fix Markdown formatting in `dynamic_programming/kadane2.cpp` (#2276)
* docs: fix grammatical errors and typos (#2201)
* docs: fix grammatical errors and typos
* compilation error fixed
* Revert "compilation error fixed"
This reverts commit 0083cbfd1a.
* feat: added physics directory and ground to ground projectile motion algorithm (#2279)
* feat: added physics folder, ground to ground projectile motion calculations
* feat: added max height function
* fix: bug in angle calculations
* test: added test cases
* docs: added comments to test case variables
* docs: added comments to calculations
* fix: changed floats to doubles
* updating DIRECTORY.md
* Update physics/ground_to_ground_projectile_motion.cpp
Co-authored-by: David Leal <halfpacho@gmail.com>
* chore: add missing namespace
* rerun checks
Co-authored-by: David <Panquesito7@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
* docs: updated a logically wrong doc comment (#2329)
* feat: add CMakeLists to the `divide_and_conquer` directory (#2072)
* fix: stairs pattern not printing slash (#2111)
when we propose 2 backslash then it doesn't take it as a comment and gets printed
Co-authored-by: David Leal <halfpacho@gmail.com>
* fix: use FreeGlut newest GitHub link (#2397)
* updating DIRECTORY.md
* fix: use FreeGlut newest GitHub link
* chore(fix): `data_strcutres` -> `data_structures` (#2399)
* feat: add Find non repeating number implementation (#2061)
* add find_single_number
* add fix issues
* remove .vscode
* add .vscode
* Update .vscode/settings.json
Co-authored-by: David Leal <halfpacho@gmail.com>
* chore(fix): minor issues
Co-authored-by: David Leal <halfpacho@gmail.com>
* [feat/docs]: improve the `quick_sort.cpp` algorithm (#2396)
* [feat/docs]: improve the `quick_sort.cpp`...
...algorithm implementation.
* clang-format and clang-tidy fixes for 40c858ba
* chore(fix): add original author
* updating DIRECTORY.md
* chore: update Discord links (#2407)
* docs: remove unneeded Markdown header
* feat: improve the Awesome Workflow (#2408)
* fix: Awesome Workflow issues
Thanks to @tjgurwara99 for the original fix: TheAlgorithms/C#1176
* chore: apply suggestions from code review
Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
* feat: various improvements
* chore: apply suggestions from code review
Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
* chore: remove LGTM and fix...
...CodeQL badges.
* docs: add guide on integrating CMake (#2410)
Taken from TheAlgorithms/C#1163
* updating DIRECTORY.md
Co-authored-by: Daemon <90456722+Daemon19@users.noreply.github.com>
Co-authored-by: aadarshkt <72285744+aadarshkt@users.noreply.github.com>
Co-authored-by: Focus <65309793+Focusucof@users.noreply.github.com>
Co-authored-by: David <Panquesito7@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: Arjit Malik <arjitmalik2001@gmail.com>
Co-authored-by: Harsh Singh <94822101+harshsingh510@users.noreply.github.com>
Co-authored-by: Ravi Dev Pandey <62198564+literalEval@users.noreply.github.com>
Co-authored-by: Mehmet <110852769+walterwhite351@users.noreply.github.com>
Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
140 lines
5.6 KiB
C++
140 lines
5.6 KiB
C++
/**
|
|
* @file
|
|
* @brief Ground to ground [projectile
|
|
* motion](https://en.wikipedia.org/wiki/Projectile_motion) equation
|
|
* implementations
|
|
* @details 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](https://github.com/Focusucof)
|
|
*/
|
|
|
|
#include <cassert> /// for assert()
|
|
#include <cmath> /// for std::pow(), std::sin(), and std::cos()
|
|
#include <iostream> /// for IO operations
|
|
|
|
/**
|
|
* @namespace physics
|
|
* @brief Physics algorithms
|
|
*/
|
|
namespace physics {
|
|
/**
|
|
* @namespace ground_to_ground_projectile_motion
|
|
* @brief Functions for the Ground to ground [projectile
|
|
* motion](https://en.wikipedia.org/wiki/Projectile_motion) equation
|
|
*/
|
|
namespace ground_to_ground_projectile_motion {
|
|
/**
|
|
* @brief Convert radians to degrees
|
|
* @param radian Angle in radians
|
|
* @param PI The definition of the constant PI
|
|
* @returns Angle in degrees
|
|
*/
|
|
double degrees_to_radians(double radian, double PI = 3.14) {
|
|
return (radian * (PI / 180));
|
|
}
|
|
|
|
/**
|
|
* @brief Calculate the time of flight
|
|
* @param initial_velocity The starting velocity of the projectile
|
|
* @param angle The angle that the projectile is launched at in degrees
|
|
* @param gravity The value used for the gravity constant
|
|
* @returns The time that the projectile is in the air for
|
|
*/
|
|
template <typename T>
|
|
T time_of_flight(T initial_velocity, T angle, double gravity = 9.81) {
|
|
double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
|
|
return 2.0 * Viy / gravity;
|
|
}
|
|
|
|
/**
|
|
* @brief Calculate the horizontal distance that the projectile travels
|
|
* @param initial_velocity The starting velocity of the projectile
|
|
* @param time The time that the projectile is in the air
|
|
* @returns Horizontal distance that the projectile travels
|
|
*/
|
|
template <typename T>
|
|
T horizontal_range(T initial_velocity, T angle, T time) {
|
|
double Vix = initial_velocity * (std::cos(degrees_to_radians(angle))); // calculate x component of the initial velocity
|
|
return Vix * time;
|
|
}
|
|
|
|
/**
|
|
* @brief Calculate the max height of the projectile
|
|
* @param initial_velocity The starting velocity of the projectile
|
|
* @param angle The angle that the projectile is launched at in degrees
|
|
* @param gravity The value used for the gravity constant
|
|
* @returns The max height that the projectile reaches
|
|
*/
|
|
template <typename T>
|
|
T max_height(T initial_velocity, T angle, double gravity = 9.81) {
|
|
double Viy = initial_velocity * (std::sin(degrees_to_radians(angle))); // calculate y component of the initial velocity
|
|
return (std::pow(Viy, 2) / (2.0 * gravity));
|
|
}
|
|
} // namespace ground_to_ground_projectile_motion
|
|
} // namespace physics
|
|
|
|
/**
|
|
* @brief Self-test implementations
|
|
* @returns void
|
|
*/
|
|
static void test() {
|
|
// initial input variables
|
|
double initial_velocity = 5.0; // double initial_velocity input
|
|
double angle = 40.0; // double angle input
|
|
|
|
// 1st test
|
|
double expected_time_of_flight = 0.655; // expected time output
|
|
double flight_time_output =
|
|
std::round(physics::ground_to_ground_projectile_motion::time_of_flight(initial_velocity, angle) * 1000.0) /
|
|
1000.0; // round output to 3 decimal places
|
|
|
|
std::cout << "Projectile Flight Time (double)" << std::endl;
|
|
std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
|
|
std::cout << "Input Angle: " << angle << std::endl;
|
|
std::cout << "Expected Output: " << expected_time_of_flight << std::endl;
|
|
std::cout << "Output: " << flight_time_output << std::endl;
|
|
assert(flight_time_output == expected_time_of_flight);
|
|
std::cout << "TEST PASSED" << std::endl << std::endl;
|
|
|
|
// 2nd test
|
|
double expected_horizontal_range = 2.51; // expected range output
|
|
double horizontal_range_output =
|
|
std::round(physics::ground_to_ground_projectile_motion::horizontal_range(initial_velocity, angle,
|
|
flight_time_output) *
|
|
100.0) /
|
|
100.0; // round output to 2 decimal places
|
|
|
|
std::cout << "Projectile Horizontal Range (double)" << std::endl;
|
|
std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
|
|
std::cout << "Input Angle: " << angle << std::endl;
|
|
std::cout << "Input Time Of Flight: " << flight_time_output << std::endl;
|
|
std::cout << "Expected Output: " << expected_horizontal_range << std::endl;
|
|
std::cout << "Output: " << horizontal_range_output << std::endl;
|
|
assert(horizontal_range_output == expected_horizontal_range);
|
|
std::cout << "TEST PASSED" << std::endl << std::endl;
|
|
|
|
// 3rd test
|
|
double expected_max_height = 0.526; // expected height output
|
|
double max_height_output =
|
|
std::round(physics::ground_to_ground_projectile_motion::max_height(initial_velocity, angle) * 1000.0) /
|
|
1000.0; // round output to 3 decimal places
|
|
|
|
std::cout << "Projectile Max Height (double)" << std::endl;
|
|
std::cout << "Input Initial Velocity: " << initial_velocity << std::endl;
|
|
std::cout << "Input Angle: " << angle << std::endl;
|
|
std::cout << "Expected Output: " << expected_max_height << std::endl;
|
|
std::cout << "Output: " << max_height_output << std::endl;
|
|
assert(max_height_output == expected_max_height);
|
|
std::cout << "TEST PASSED" << std::endl << std::endl;
|
|
}
|
|
|
|
/**
|
|
* @brief Main function
|
|
* @returns 0 on exit
|
|
*/
|
|
int main() {
|
|
test(); // run self-test implementations
|
|
return 0;
|
|
}
|