/
vector3.txx
72 lines (56 loc) · 1.91 KB
/
vector3.txx
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
#pragma once
template <class T> Vector3<T>::Vector3(T x, T y, T z) {
this->val[0] = x;
this->val[1] = y;
this->val[2] = z;
}
template <class T> Vector3<T>::Vector3() {
}
template <class T> Vector3<T>::Vector3(T* v) {
this->val[0] = v[0];
this->val[1] = v[1];
this->val[2] = v[2];
}
template <class T> T& Vector3<T>::operator[](unsigned int i) {
return this->val[i];
}
template <class T> T Vector3<T>::X() const {
return this->val[0];
}
template <class T> T Vector3<T>::Y() const {
return this->val[1];
}
template <class T> T Vector3<T>::Z() const {
return this->val[2];
}
template <class T> T Vector3<T>::getNorm() const {
return sqrt(this->val[0] * this->val[0] + this->val[1] * this->val[1] + this->val[2] * this->val[2]);
}
template <class T> void Vector3<T>::makeUnitVector() {
T norme = this->getNorm();
this->val[0] /= norme;
this->val[1] /= norme;
this->val[2] /= norme;
}
template <class T> T Vector3<T>::operator*(const Vector3<T> &op) const {
return this->val[0] * op.val[0] + this->val[1] * op.val[1] + this->val[2] * op.val[2];
}
template <class T> Vector3<T> Vector3<T>::operator*(T op) const {
return Vector3<T>(this->val[0] * op, this->val[1] * op, this->val[2] * op);
}
template <class T> Vector3<T> Vector3<T>::operator+(const Vector3<T> &op) const {
return Vector3<T>(this->val[0] + op.val[0], this->val[1] + op.val[1], this->val[2] + op.val[2]);
}
template <class T>
Vector3<T> &Vector3<T>::operator+=(const Vector3<T> &v) {
for (int i = 0; i < 3; i++) {
this->val[i] += v.val[i];
}
return *this;
}
template <class T> Vector3<T> Vector3<T>::operator-(const Vector3<T> &op) const {
return Vector3<T>(this->val[0] - op.val[0], this->val[1] - op.val[1], this->val[2] - op.val[2]);
}
template <class T> std::ostream &operator<<(std::ostream &s, const Vector3<T> &t) {
return s << t.X() << " " << t.Y() << " " << t.Z();
}