-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
94 lines (77 loc) · 1.65 KB
/
Vector.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include <iostream>
#include <cmath>
using namespace std;
class Vector
{
public:
double x, y, z;
// constructeur
Vector(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z){};
// affichage
void print()
{
cout << "x: " << x << "\ty: " << y << "\tz: " << z << endl;
}
// opérateurs simples
Vector operator+(const Vector &a)
{
return Vector(x + a.x, y + a.y, z + a.z);
};
Vector operator-(const Vector &a)
{
return Vector(x - a.x, y - a.y, z - a.z);
};
Vector operator*(double a)
{
return Vector(a * x, a * y, a * z);
};
// multiplication terme à terme
Vector operator*(Vector &V)
{
return Vector(V.x * x, V.y * y, V.z * z);
};
Vector operator/(double a)
{
return Vector(x / a, y / a, z / a);
};
// opérateurs double
Vector operator+=(const Vector b)
{
x += b.x;
y += b.y;
z += b.z;
return *this;
}
// opérateurs produits
double dot(const Vector &a)
{
return a.x * x + a.y * y + a.z * z;
};
Vector cross(const Vector &a)
// produit vectoriel
{
return Vector(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x);
}
double getNorm()
{
return sqrt(x * x + y * y + z * z);
}
double getNorm2()
{
return (x * x + y * y + z * z);
}
void normalize()
{
double norm = getNorm();
x /= norm;
y /= norm;
z /= norm;
}
Vector getNormalized()
{
Vector result(*this);
result.normalize();
return result;
}
};