TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Calculates the Cross Product and the magnitude of two mathematical 3D vectors. More...
#include <iostream>
#include <array>
#include <cmath>
#include <cassert>
Go to the source code of this file.
Namespaces | |
namespace | math |
for assert | |
namespace | vector_cross |
Functions for Vector Cross Product algorithms. | |
Functions | |
std::array< double, 3 > | math::vector_cross::cross (const std::array< double, 3 > &A, const std::array< double, 3 > &B) |
Function to calculate the cross product of the passed arrays containing the direction ratios of the two mathematical vectors. | |
double | math::vector_cross::mag (const std::array< double, 3 > &vec) |
Calculates the magnitude of the mathematical vector from it's direction ratios. | |
static void | test () |
namespace math | |
int | main () |
Main Function. | |
Calculates the Cross Product and the magnitude of two mathematical 3D vectors.
Cross Product of two vectors gives a vector. Direction Ratios of a vector are the numeric parts of the given vector. They are the tree parts of the vector which determine the magnitude (value) of the vector. The method of finding a cross product is the same as finding the determinant of an order 3 matrix consisting of the first row with unit vectors of magnitude 1, the second row with the direction ratios of the first vector and the third row with the direction ratios of the second vector. The magnitude of a vector is it's value expressed as a number. Let the direction ratios of the first vector, P be: a, b, c Let the direction ratios of the second vector, Q be: x, y, z Therefore the calculation for the cross product can be arranged as:
The direction ratios (DR) are calculated as follows: 1st DR, J: (b * z) - (c * y) 2nd DR, A: -((a * z) - (c * x)) 3rd DR, N: (a * y) - (b * x)
Therefore, the direction ratios of the cross product are: J, A, N The following C++ Program calculates the direction ratios of the cross products of two vector. The program uses a function, cross() for doing so. The direction ratios for the first and the second vector has to be passed one by one seperated by a space character.
Magnitude of a vector is the square root of the sum of the squares of the direction ratios.
An example of a running instance of the executable program:
Pass the first Vector: 1 2 3 Pass the second Vector: 4 5 6 The cross product is: -3 6 -3 Magnitude: 7.34847
Definition in file vector_cross_product.cpp.
std::array< double, 3 > math::vector_cross::cross | ( | const std::array< double, 3 > & | A, |
const std::array< double, 3 > & | B ) |
Function to calculate the cross product of the passed arrays containing the direction ratios of the two mathematical vectors.
A | contains the direction ratios of the first mathematical vector. |
B | contains the direction ration of the second mathematical vector. |
Performs the cross product as shown in @algorithm.
Definition at line 69 of file vector_cross_product.cpp.
double math::vector_cross::mag | ( | const std::array< double, 3 > & | vec | ) |
Calculates the magnitude of the mathematical vector from it's direction ratios.
vec | an array containing the direction ratios of a mathematical vector. |
Definition at line 83 of file vector_cross_product.cpp.
int main | ( | void | ) |
Main Function.
Asks the user to enter the direction ratios for each of the two mathematical vectors using std::cin
Tests the functions with sample input before asking for user input.
Gets the values for the first vector.
Gets the values for the second vector.
Displays the output out.
Displays the magnitude of the cross product.
Definition at line 109 of file vector_cross_product.cpp.
|
static |
namespace math
test function.
test the cross() and the mag() functions.
Tests the cross() function.
Tests the mag() function.
Definition at line 94 of file vector_cross_product.cpp.