Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
vector_cross_product.cpp File Reference

Calculates the Cross Product and the magnitude of two mathematical 3D vectors. More...

#include <iostream>
#include <array>
#include <cmath>
#include <cassert>
Include dependency graph for vector_cross_product.cpp:

Namespaces

namespace  math
 for IO operations
 
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.
 

Detailed Description

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:

P x Q:
1 1 1
a b c
x y z

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.

Example:

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

Author
Shreyas Sable

Function Documentation

◆ cross()

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.

Parameters
Acontains the direction ratios of the first mathematical vector.
Bcontains the direction ration of the second mathematical vector.
Returns
the direction ratios of the cross product.

Performs the cross product as shown in @algorithm.

69 {
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 }
Here is the call graph for this function:

◆ mag()

double math::vector_cross::mag ( const std::array< double, 3 > & vec)

Calculates the magnitude of the mathematical vector from it's direction ratios.

Parameters
vecan array containing the direction ratios of a mathematical vector.
Returns
type: double description: the magnitude of the mathematical vector from the given direction ratios.
83 {
84 double magnitude = sqrt((vec[0] * vec[0]) + (vec[1] * vec[1]) + (vec[2] * vec[2]));
85 return magnitude;
86 }
T sqrt(T... args)
Here is the call graph for this function:

◆ main()

int main ( void )

Main Function.

Asks the user to enter the direction ratios for each of the two mathematical vectors using std::cin

Returns
0 on exit

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.

109 {
110
111 /// Tests the functions with sample input before asking for user input.
112 test();
113
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.
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;
133}
T endl(T... args)
std::array< double, 3 > 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 t...
Definition vector_cross_product.cpp:69
double mag(const std::array< double, 3 > &vec)
Calculates the magnitude of the mathematical vector from it's direction ratios.
Definition vector_cross_product.cpp:83
static void test()
namespace math
Definition vector_cross_product.cpp:94
Here is the call graph for this function:

◆ test()

static void test ( )
static

namespace math

test function.

test the cross() and the mag() functions.

Tests the cross() function.

Tests the mag() function.

94 {
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);
98
99 /// Tests the mag() function.
100 double t_mag = math::vector_cross::mag({6, 8, 0});
101 assert(t_mag == 10);
102}