TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
vector_cross_product.cpp
Go to the documentation of this file.
1
48#include <iostream>
49#include <array>
50#include <cmath>
51#include <cassert>
52
57namespace math {
62 namespace vector_cross {
69 std::array<double, 3> cross(const std::array<double, 3> &A, const std::array<double, 3> &B) {
70 std::array<double, 3> product;
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 }
77
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 }
88}
89
94static void test() {
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
100 double t_mag = math::vector_cross::mag({6, 8, 0});
101 assert(t_mag == 10);
102}
103
109int main() {
110
112 test();
113
114 std::array<double, 3> vec1;
115 std::array<double, 3> vec2;
116
118 std::cout << "\nPass the first Vector: ";
119 std::cin >> vec1[0] >> vec1[1] >> vec1[2];
120
122 std::cout << "\nPass the second Vector: ";
123 std::cin >> vec2[0] >> vec2[1] >> vec2[2];
124
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
130 std::cout << "Magnitude: " << math::vector_cross::mag(product) << "\n" << std::endl;
131
132 return 0;
133}
for assert
Functions for Vector Cross Product algorithms.
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...
double 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.