Skip to content

Commit b9c118f

Browse files
vil02github-actions[bot]realstealthninja
authored
style: remove interaction with the user (#3009)
* style: remove interaction with the user Helps with #2753 * clang-format and clang-tidy fixes for ff441e0 * tests: add test with result being a zero vector Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
1 parent ba0d3ff commit b9c118f

File tree

1 file changed

+75
-76
lines changed

1 file changed

+75
-76
lines changed

math/vector_cross_product.cpp

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
/**
22
* @file
33
*
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.
57
*
68
*
79
* @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:
1720
*
1821
* ```
1922
* P x Q:
@@ -28,11 +31,13 @@
2831
* 3rd DR, N: (a * y) - (b * x)
2932
*
3033
* 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.
3438
*
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.
3641
*
3742
* ### Example:
3843
* An example of a running instance of the executable program:
@@ -45,89 +50,83 @@
4550
* @author [Shreyas Sable](https://github.com/Shreyas-OwO)
4651
*/
4752

48-
#include <iostream>
4953
#include <array>
50-
#include <cmath>
5154
#include <cassert>
55+
#include <cmath>
5256

5357
/**
5458
* @namespace math
5559
* @brief Math algorithms
5660
*/
5761
namespace math {
58-
/**
59-
* @namespace vector_cross
60-
* @brief Functions for Vector Cross Product algorithms
61-
*/
62-
namespace vector_cross {
63-
/**
64-
* @brief Function to calculate the cross product of the passed arrays containing the direction ratios of the two mathematical vectors.
65-
* @param A contains the direction ratios of the first mathematical vector.
66-
* @param B contains the direction ration of the second mathematical vector.
67-
* @returns the direction ratios of the cross product.
68-
*/
69-
std::array<double, 3> cross(const std::array<double, 3> &A, const std::array<double, 3> &B) {
70-
std::array<double, 3> product;
71-
/// Performs the cross product as shown in @algorithm.
72-
product[0] = (A[1] * B[2]) - (A[2] * B[1]);
73-
product[1] = -((A[0] * B[2]) - (A[2] * B[0]));
74-
product[2] = (A[0] * B[1]) - (A[1] * B[0]);
75-
return product;
76-
}
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+
}
7783

78-
/**
79-
* @brief Calculates the magnitude of the mathematical vector from it's direction ratios.
80-
* @param vec an array containing the direction ratios of a mathematical vector.
81-
* @returns type: double description: the magnitude of the mathematical vector from the given direction ratios.
82-
*/
83-
double mag(const std::array<double, 3> &vec) {
84-
double magnitude = sqrt((vec[0] * vec[0]) + (vec[1] * vec[1]) + (vec[2] * vec[2]));
85-
return magnitude;
86-
}
87-
} /// namespace vector_cross
88-
} /// 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
8998

9099
/**
91100
* @brief test function.
92101
* @details test the cross() and the mag() functions.
93102
*/
94103
static void test() {
95-
/// Tests the cross() function.
96-
std::array<double, 3> t_vec = math::vector_cross::cross({1, 2, 3}, {4, 5, 6});
97-
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);
108+
109+
/// Tests the mag() function.
110+
double t_mag = math::vector_cross::mag({6, 8, 0});
111+
assert(t_mag == 10);
98112

99-
/// Tests the mag() function.
100-
double t_mag = math::vector_cross::mag({6, 8, 0});
101-
assert(t_mag == 10);
113+
/// Tests A ⨯ A = 0
114+
std::array<double, 3> t_vec2 =
115+
math::vector_cross::cross({1, 2, 3}, {1, 2, 3});
116+
assert(t_vec2[0] == 0 && t_vec2[1] == 0 &&
117+
t_vec2[2] == 0); // checking each element
118+
assert(math::vector_cross::mag(t_vec2) ==
119+
0); // checking the magnitude is also zero
102120
}
103121

104122
/**
105123
* @brief Main Function
106-
* @details Asks the user to enter the direction ratios for each of the two mathematical vectors using std::cin
124+
* @details Asks the user to enter the direction ratios for each of the two
125+
* mathematical vectors using std::cin
107126
* @returns 0 on exit
108127
*/
109128
int main() {
110-
111-
/// Tests the functions with sample input before asking for user input.
112-
test();
113-
114-
std::array<double, 3> vec1;
115-
std::array<double, 3> vec2;
116-
117-
/// Gets the values for the first vector.
118-
std::cout << "\nPass the first Vector: ";
119-
std::cin >> vec1[0] >> vec1[1] >> vec1[2];
120-
121-
/// Gets the values for the second vector.
122-
std::cout << "\nPass the second Vector: ";
123-
std::cin >> vec2[0] >> vec2[1] >> vec2[2];
124-
125-
/// Displays the output out.
126-
std::array<double, 3> product = math::vector_cross::cross(vec1, vec2);
127-
std::cout << "\nThe cross product is: " << product[0] << " " << product[1] << " " << product[2] << std::endl;
128-
129-
/// Displays the magnitude of the cross product.
130-
std::cout << "Magnitude: " << math::vector_cross::mag(product) << "\n" << std::endl;
131-
132-
return 0;
129+
/// Tests the functions with sample input before asking for user input.
130+
test();
131+
return 0;
133132
}

0 commit comments

Comments
 (0)