diff --git a/math/area.cpp b/math/area.cpp index a787e2e3a4..3c95b1cb4f 100644 --- a/math/area.cpp +++ b/math/area.cpp @@ -121,6 +121,29 @@ template T hemi_sphere_surface_area(T radius) { return 3 * M_PI * pow(radius, 2); } + +/** + * @brief area of a [rhombus](https://en.wikipedia.org/wiki/Rhombus) (d1 * d2 / 2) + * @param diagonal1 is the length of the first diagonal + * @param diagonal2 is the length of the second diagonal + * @returns area of the rhombus + */ +template +T rhombus_area(T diagonal1, T diagonal2) { + return (diagonal1 * diagonal2) / 2; +} + +/** + * @brief area of a [trapezoid](https://en.wikipedia.org/wiki/Trapezoid) ((a + b) * h / 2) + * @param base1 is the length of the top parallel side + * @param base2 is the length of the bottom parallel side + * @param height is the perpendicular distance between the bases + * @returns area of the trapezoid + */ +template +T trapezoid_area(T base1, T base2, T height) { + return ((base1 + base2) * height) / 2; +} } // namespace math /** @@ -291,6 +314,33 @@ static void test() { std::cout << "Output: " << double_area << std::endl; assert(double_area == double_expected); std::cout << "TEST PASSED" << std::endl << std::endl; + + // 12th test (Rhombus) + int_base = 8; // Using existing int variables for diagonal 1 + int_height = 10; // Using existing int variables for diagonal 2 + int_expected = 40; // (8 * 10) / 2 + int_area = math::rhombus_area(int_base, int_height); + + std::cout << "AREA OF A RHOMBUS" << std::endl; + std::cout << "Input Diagonal 1: " << int_base << " Diagonal 2: " << int_height << std::endl; + std::cout << "Expected Output: " << int_expected << std::endl; + std::cout << "Output: " << int_area << std::endl; + assert(int_area == int_expected); + std::cout << "TEST PASSED" << std::endl << std::endl; + + // 13th test (Trapezoid) + double_length = 5.0; // base 1 + double_width = 9.0; // base 2 + double_height = 4.0; // height + double_expected = 28.0; // ((5 + 9) * 4) / 2 + double_area = math::trapezoid_area(double_length, double_width, double_height); + + std::cout << "AREA OF A TRAPEZOID" << std::endl; + std::cout << "Input Base 1: " << double_length << " Base 2: " << double_width << " Height: " << double_height << std::endl; + std::cout << "Expected Output: " << double_expected << std::endl; + std::cout << "Output: " << double_area << std::endl; + assert(double_area == double_expected); + std::cout << "TEST PASSED" << std::endl << std::endl; } /** diff --git a/math/vector_dot_product.cpp b/math/vector_dot_product.cpp new file mode 100644 index 0000000000..b1287d6c6b --- /dev/null +++ b/math/vector_dot_product.cpp @@ -0,0 +1,69 @@ +/** + * @file + * + * @brief Calculates the [Dot Product](https://en.wikipedia.org/wiki/Dot_product) of two 3D mathematical vectors. + * + * + * @details The dot product (also known as the scalar product) takes two coordinate vectors + * and returns a single scalar value. It is calculated by multiplying matching components + * and taking the sum of those products. + * * For two vectors $A = [a_1, a_2, a_3]$ and $B = [b_1, b_2, b_3]$, the calculation is: + * $$A \cdot B = (a_1 \cdot b_1) + (a_2 \cdot b_2) + (a_3 \cdot b_3)$$ + * + * @author [Manraj Singh Sandhu](https://github.com/manraj-singh-sandhu) + */ + + #include + #include + +/** + * @namespace math + * @brief Math algorithms + */ + +namespace math{ + /** + * @namespace vector_dot + * @brief Functions for Vector Dot Product algorithms + */ +namespace vector_dot{ + /** + * @brief Computes the dot product of two 3D arrays containing component coordinates. + * @param A contains the components of the first mathematical vector. + * @param B contains the components of the second mathematical vector. + * @returns The scalar dot product value as a double. + */ + + double dot(const std::array &A, const std::array &B){ + return (A[0] * B[0] ) + (A[1] * B[1]) + (A[2] * B[2]); + } +} //namespace vector_dot +} //namespace math + +/** + * @brief Self-test function to validate calculations. + */ + + static void test(){ + //Test Case 1: Standard positive vectors + //[1, 3, -5] . [4, -2, -1] -> (1*4) + (3*-2) + (-5*-1) = 4 - 6 + 5 = 3 + std::array v1 = {1,3,-5}; + std::array v2 = {4, -2, -1}; + assert(math::vector_dot::dot(v1, v2) == 3); + + // Test Case 2: Perpendicular vectors (Dot product must be 0) + // [1, 0, 0] . [0, 1, 0] -> 0 + std::array v3 = {1, 0, 0}; + std::array v4 = {0, 1, 0}; + assert(math::vector_dot::dot(v3, v4) == 0); + } + + /** + * @brief Main funtion running verification tests. + * @returns 0 on successful validation execution. + */ + + int main(){ + test(); // Executes assertions + return 0; + } diff --git a/math/volume.cpp b/math/volume.cpp index 87bfa6a825..541a00f02b 100644 --- a/math/volume.cpp +++ b/math/volume.cpp @@ -103,6 +103,43 @@ template T cylinder_volume(T radius, T height, double PI = 3.14) { return PI * std::pow(radius, 2) * height; } + +/** + * @brief The volume of a [hemi-sphere](https://en.wikipedia.org/wiki/Hemisphere) + * @param radius The radius of the hemi-sphere + * @param PI The definition of the constant PI + * @returns The volume of the hemi-sphere + */ +template +T hemisphere_volume(T radius, double PI = 3.14) { + return PI * std::pow(radius, 3) * 2 / 3; +} + +/** + * @brief The volume of a [capsule](https://en.wikipedia.org/wiki/Capsule_(geometry)) + * @details A capsule is formed by joining two hemispheres to the ends of a cylinder. + * @param radius The radius of the cylindrical and hemispherical parts + * @param height The height of the cylindrical part only + * @param PI The definition of the constant PI + * @returns The volume of the capsule + */ +template +T capsule_volume(T radius, T height, double PI = 3.14) { + return PI * std::pow(radius, 2) * ((4.0 / 3.0) * radius + height); +} + +/** + * @brief The volume of a [torus](https://en.wikipedia.org/wiki/Torus) + * @details A torus is a doughnut-shaped object. + * @param small_radius The radius of the tube (inner circle radius) + * @param large_radius The distance from the center of the torus to the center of the tube + * @param PI The definition of the constant PI + * @returns The volume of the torus + */ +template +T torus_volume(T small_radius, T large_radius, double PI = 3.14) { + return 2 * std::pow(PI, 2) * large_radius * std::pow(small_radius, 2); +} } // namespace math /** @@ -226,6 +263,46 @@ static void test() { std::cout << "Output: " << double_volume << std::endl; assert(double_volume == double_expected); std::cout << "TEST PASSED" << std::endl << std::endl; + + // 8th test + double_radius = 3.0; + double_expected = 56.52; // (3.14 * 3^3 * 2) / 3 = 56.52 + double_volume = math::hemisphere_volume(double_radius); + + std::cout << "VOLUME OF A HEMI-SPHERE" << std::endl; + std::cout << "Input Radius: " << double_radius << std::endl; + std::cout << "Expected Output: " << double_expected << std::endl; + std::cout << "Output: " << double_volume << std::endl; + assert(double_volume == double_expected); + std::cout << "TEST PASSED" << std::endl << std::endl; + + // 9th test (Capsule) + double_radius = 3.0; + double_height = 5.0; + // Calculation: 3.14 * 3^2 * ((4/3)*3 + 5) = 28.26 * (4 + 5) = 254.34 + double_expected = 254.34; + double_volume = math::capsule_volume(double_radius, double_height); + + std::cout << "VOLUME OF A CAPSULE" << std::endl; + std::cout << "Input Radius: " << double_radius << " Height: " << double_height << std::endl; + std::cout << "Expected Output: " << double_expected << std::endl; + std::cout << "Output: " << double_volume << std::endl; + assert(std::abs(double_volume - double_expected) < 0.0001); // accounting for small floating point variations + std::cout << "TEST PASSED" << std::endl << std::endl; + + // 10th test (Torus) + double_radius = 2.0; // small_radius (r) + double_height = 6.0; // using double_height as large_radius (R) + // Calculation: 2 * 3.14^2 * 6 * 2^2 = 2 * 9.8596 * 6 * 4 = 473.2608 + double_expected = 473.2608; + double_volume = math::torus_volume(double_radius, double_height); + + std::cout << "VOLUME OF A TORUS" << std::endl; + std::cout << "Input Inner Radius: " << double_radius << " Outer Radius: " << double_height << std::endl; + std::cout << "Expected Output: " << double_expected << std::endl; + std::cout << "Output: " << double_volume << std::endl; + assert(std::abs(double_volume - double_expected) < 0.0001); + std::cout << "TEST PASSED" << std::endl << std::endl; } /**