-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.cpp
More file actions
112 lines (93 loc) · 1.63 KB
/
vec3.cpp
File metadata and controls
112 lines (93 loc) · 1.63 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
using namespace std;
template <class T>
class vec3
{
public:
vec3() { _x = 0, _y = 0; _z = 0; }
vec3(T x, T y, T z)
{
_x = x;
_y = y;
_z = z;
}
vec3(const vec3& vec)
{
_x = vec._x;
_y = vec._y;
_z = vec._z;
}
vec3 operator+(const vec3& vec) const
{
return vec3(_x + vec._x, _y + vec._y, _z + vec._z);
}
vec3 operator-(const vec3& vec) const
{
return vec3(_x - vec._x, _y - vec._y, _z - vec._z);
}
vec3 operator+=(const vec3& vec) const
{
_x += vec._x;
_y += vec._y;
_z += vec._z;
}
vec3 operator-=(const vec3& vec) const
{
_x -= vec._x;
_y -= vec._y;
_z -= vec._z;
}
vec3 operator=(const vec3& vec) const
{
_x = vec._x;
_y = vec._y;
_z = vec._z;
}
bool operator ==(const vec3& vec)
{
return vec._x == _x && vec._y == _y && vec._z == _z;
}
void Normalize()
{
// magnitud vec
T magnitud = sqrt((_x * _x) + (_y * _y) + (_z * _z));
// mentres no sigui zero
if (magnitud != 0)
{
_x /= magnitud;
_y /= magnitud;
_z /= magnitud;
}
}
void Zero()
{
_x = _y = _z = 0;
}
void isZero()
{
return _x == 0 && _y == 0 && _z == 0;
}
int DistanceTo(vec3 vec)
{
T distX, distY, distZ;
distX = vec._x - _x;
distY = vec._y - _y;
distZ = vec._z - _z;
return sqrt((distX * distX) + (distY * distY) + (distZ * distZ));
}
private:
T _x;
T _y;
T _z;
};
int main()
{
vec3 <int> a;
vec3 <int> b(1,2,3);
vec3 <int> c(a);
cout << a.DistanceTo(b) << endl;
b.Zero();
cout << a.DistanceTo(b) << endl;
system("pause");
return 0;
}