1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
struct Vector {
float n1 {}, n2 {}, n3 {};
};
Vector generate_vector(float a, float b, float c)
{
return Vector {a, b, c};
}
void show(const Vector& v)
{
cout << setprecision(6) << fixed;
cout << "<" << v.n1 << ", " << v.n2 << ", " << v.n3 << ">\n";
}
float dot_product(const Vector& v1, const Vector& v2)
{
return v1.n1 * v2.n1 + v1.n2 * v2.n2 + v1.n3 * v2.n3;
}
Vector normalize(const Vector& v)
{
const auto magnitude {sqrt(v.n1 * v.n1 + v.n2 * v.n2 + v.n3 * v.n3)};
return Vector {v.n1 / magnitude, v.n2 / magnitude, v.n3 / magnitude};
}
Vector const_multiply(float cons, const Vector& v)
{
return Vector {v.n1 * cons, v.n2 * cons, v.n3 * cons};
}
Vector cross_product(const Vector& v1, const Vector& v2)
{
return Vector {(v1.n2 * v2.n3) - (v1.n3 * v2.n2), (v1.n3 * v2.n1) - (v1.n1 * v2.n3), (v1.n1 * v2.n2) - (v1.n2 * v2.n1)};
}
Vector add(const Vector& v1, const Vector& v2)
{
return Vector {v1.n1 + v2.n1, v1.n2 + v2.n2, v1.n3 + v2.n3};
}
int main()
{
const auto v1 {generate_vector(2, 1, 3)};
const auto v2 {generate_vector(4, -5, 8)};
show(v1);
show(v2);
const auto dp {dot_product(v1, v2)};
cout << dp << '\n';
show(normalize(v1));
show(const_multiply(5, v1));
show(cross_product(v1, v2));
show(add(v1, v2));
}
|