|
1 | 1 | /**
|
2 | 2 | * @file
|
3 | 3 | *
|
4 |
| - * @brief Calculates the [Cross Product](https://en.wikipedia.org/wiki/Cross_product) and the magnitude of two mathematical 3D vectors. |
| 4 | + * @brief Calculates the [Cross |
| 5 | + *Product](https://en.wikipedia.org/wiki/Cross_product) and the magnitude of two |
| 6 | + *mathematical 3D vectors. |
5 | 7 | *
|
6 | 8 | *
|
7 | 9 | * @details Cross Product of two vectors gives a vector.
|
8 |
| - * Direction Ratios of a vector are the numeric parts of the given vector. They are the tree parts of the |
9 |
| - * vector which determine the magnitude (value) of the vector. |
10 |
| - * The method of finding a cross product is the same as finding the determinant of an order 3 matrix consisting |
11 |
| - * of the first row with unit vectors of magnitude 1, the second row with the direction ratios of the |
12 |
| - * first vector and the third row with the direction ratios of the second vector. |
13 |
| - * The magnitude of a vector is it's value expressed as a number. |
14 |
| - * Let the direction ratios of the first vector, P be: a, b, c |
15 |
| - * Let the direction ratios of the second vector, Q be: x, y, z |
16 |
| - * Therefore the calculation for the cross product can be arranged as: |
| 10 | + * Direction Ratios of a vector are the numeric parts of the given vector. They |
| 11 | + *are the tree parts of the vector which determine the magnitude (value) of the |
| 12 | + *vector. The method of finding a cross product is the same as finding the |
| 13 | + *determinant of an order 3 matrix consisting of the first row with unit vectors |
| 14 | + *of magnitude 1, the second row with the direction ratios of the first vector |
| 15 | + *and the third row with the direction ratios of the second vector. The |
| 16 | + *magnitude of a vector is it's value expressed as a number. Let the direction |
| 17 | + *ratios of the first vector, P be: a, b, c Let the direction ratios of the |
| 18 | + *second vector, Q be: x, y, z Therefore the calculation for the cross product |
| 19 | + *can be arranged as: |
17 | 20 | *
|
18 | 21 | * ```
|
19 | 22 | * P x Q:
|
|
28 | 31 | * 3rd DR, N: (a * y) - (b * x)
|
29 | 32 | *
|
30 | 33 | * Therefore, the direction ratios of the cross product are: J, A, N
|
31 |
| - * The following C++ Program calculates the direction ratios of the cross products of two vector. |
32 |
| - * The program uses a function, cross() for doing so. |
33 |
| - * The direction ratios for the first and the second vector has to be passed one by one seperated by a space character. |
| 34 | + * The following C++ Program calculates the direction ratios of the cross |
| 35 | + *products of two vector. The program uses a function, cross() for doing so. The |
| 36 | + *direction ratios for the first and the second vector has to be passed one by |
| 37 | + *one seperated by a space character. |
34 | 38 | *
|
35 |
| - * Magnitude of a vector is the square root of the sum of the squares of the direction ratios. |
| 39 | + * Magnitude of a vector is the square root of the sum of the squares of the |
| 40 | + *direction ratios. |
36 | 41 | *
|
37 | 42 | * ### Example:
|
38 | 43 | * An example of a running instance of the executable program:
|
|
46 | 51 | */
|
47 | 52 |
|
48 | 53 | #include <array>
|
49 |
| -#include <cmath> |
50 | 54 | #include <cassert>
|
| 55 | +#include <cmath> |
51 | 56 |
|
52 | 57 | /**
|
53 | 58 | * @namespace math
|
54 | 59 | * @brief Math algorithms
|
55 | 60 | */
|
56 | 61 | namespace math {
|
57 |
| - /** |
58 |
| - * @namespace vector_cross |
59 |
| - * @brief Functions for Vector Cross Product algorithms |
60 |
| - */ |
61 |
| - namespace vector_cross { |
62 |
| - /** |
63 |
| - * @brief Function to calculate the cross product of the passed arrays containing the direction ratios of the two mathematical vectors. |
64 |
| - * @param A contains the direction ratios of the first mathematical vector. |
65 |
| - * @param B contains the direction ration of the second mathematical vector. |
66 |
| - * @returns the direction ratios of the cross product. |
67 |
| - */ |
68 |
| - std::array<double, 3> cross(const std::array<double, 3> &A, const std::array<double, 3> &B) { |
69 |
| - std::array<double, 3> product; |
70 |
| - /// Performs the cross product as shown in @algorithm. |
71 |
| - product[0] = (A[1] * B[2]) - (A[2] * B[1]); |
72 |
| - product[1] = -((A[0] * B[2]) - (A[2] * B[0])); |
73 |
| - product[2] = (A[0] * B[1]) - (A[1] * B[0]); |
74 |
| - return product; |
75 |
| - } |
| 62 | +/** |
| 63 | + * @namespace vector_cross |
| 64 | + * @brief Functions for Vector Cross Product algorithms |
| 65 | + */ |
| 66 | +namespace vector_cross { |
| 67 | +/** |
| 68 | + * @brief Function to calculate the cross product of the passed arrays |
| 69 | + * containing the direction ratios of the two mathematical vectors. |
| 70 | + * @param A contains the direction ratios of the first mathematical vector. |
| 71 | + * @param B contains the direction ration of the second mathematical vector. |
| 72 | + * @returns the direction ratios of the cross product. |
| 73 | + */ |
| 74 | +std::array<double, 3> cross(const std::array<double, 3> &A, |
| 75 | + const std::array<double, 3> &B) { |
| 76 | + std::array<double, 3> product; |
| 77 | + /// Performs the cross product as shown in @algorithm. |
| 78 | + product[0] = (A[1] * B[2]) - (A[2] * B[1]); |
| 79 | + product[1] = -((A[0] * B[2]) - (A[2] * B[0])); |
| 80 | + product[2] = (A[0] * B[1]) - (A[1] * B[0]); |
| 81 | + return product; |
| 82 | +} |
76 | 83 |
|
77 |
| - /** |
78 |
| - * @brief Calculates the magnitude of the mathematical vector from it's direction ratios. |
79 |
| - * @param vec an array containing the direction ratios of a mathematical vector. |
80 |
| - * @returns type: double description: the magnitude of the mathematical vector from the given direction ratios. |
81 |
| - */ |
82 |
| - double mag(const std::array<double, 3> &vec) { |
83 |
| - double magnitude = sqrt((vec[0] * vec[0]) + (vec[1] * vec[1]) + (vec[2] * vec[2])); |
84 |
| - return magnitude; |
85 |
| - } |
86 |
| - } /// namespace vector_cross |
87 |
| -} /// namespace math |
| 84 | +/** |
| 85 | + * @brief Calculates the magnitude of the mathematical vector from it's |
| 86 | + * direction ratios. |
| 87 | + * @param vec an array containing the direction ratios of a mathematical vector. |
| 88 | + * @returns type: double description: the magnitude of the mathematical vector |
| 89 | + * from the given direction ratios. |
| 90 | + */ |
| 91 | +double mag(const std::array<double, 3> &vec) { |
| 92 | + double magnitude = |
| 93 | + sqrt((vec[0] * vec[0]) + (vec[1] * vec[1]) + (vec[2] * vec[2])); |
| 94 | + return magnitude; |
| 95 | +} |
| 96 | +} // namespace vector_cross |
| 97 | +} // namespace math |
88 | 98 |
|
89 | 99 | /**
|
90 | 100 | * @brief test function.
|
91 | 101 | * @details test the cross() and the mag() functions.
|
92 | 102 | */
|
93 | 103 | static void test() {
|
94 |
| - /// Tests the cross() function. |
95 |
| - std::array<double, 3> t_vec = math::vector_cross::cross({1, 2, 3}, {4, 5, 6}); |
96 |
| - assert(t_vec[0] == -3 && t_vec[1] == 6 && t_vec[2] == -3); |
| 104 | + /// Tests the cross() function. |
| 105 | + std::array<double, 3> t_vec = |
| 106 | + math::vector_cross::cross({1, 2, 3}, {4, 5, 6}); |
| 107 | + assert(t_vec[0] == -3 && t_vec[1] == 6 && t_vec[2] == -3); |
97 | 108 |
|
98 |
| - /// Tests the mag() function. |
99 |
| - double t_mag = math::vector_cross::mag({6, 8, 0}); |
100 |
| - assert(t_mag == 10); |
| 109 | + /// Tests the mag() function. |
| 110 | + double t_mag = math::vector_cross::mag({6, 8, 0}); |
| 111 | + assert(t_mag == 10); |
101 | 112 | }
|
102 | 113 |
|
103 | 114 | /**
|
104 | 115 | * @brief Main Function
|
105 |
| - * @details Asks the user to enter the direction ratios for each of the two mathematical vectors using std::cin |
| 116 | + * @details Asks the user to enter the direction ratios for each of the two |
| 117 | + * mathematical vectors using std::cin |
106 | 118 | * @returns 0 on exit
|
107 | 119 | */
|
108 | 120 | int main() {
|
109 |
| - |
110 |
| - /// Tests the functions with sample input before asking for user input. |
111 |
| - test(); |
112 |
| - return 0; |
| 121 | + /// Tests the functions with sample input before asking for user input. |
| 122 | + test(); |
| 123 | + return 0; |
113 | 124 | }
|
0 commit comments