-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.cpp
More file actions
249 lines (199 loc) · 3.62 KB
/
Vector.cpp
File metadata and controls
249 lines (199 loc) · 3.62 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "Vector.h"
#include <cmath>
#include "Util.h"
// vec3
void vec3::zero()
{
x = 0; y = 0; z = 0;
}
void vec3::normalize()
{
float magnitude = sqrt(x*x + y*y + z*z);
if(magnitude == 0)
return;
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
vec3 vec3::normalized()
{
vec3 ret = *this;
ret.normalize();
return ret;
}
vec3 vec3::normalize(const vec3& vec)
{
vec3 ret = vec;
ret.normalize();
return ret;
}
vec3 vec3::sign()
{
using ::sign;
return vec3(
sign(x),
sign(y),
sign(z)
);
}
vec3 vec3::abs()
{
using ::abs;
return vec3(
abs(x),
abs(y),
abs(z)
);
}
vec3 vec3::fract()
{
using ::fract;
return vec3(
fract(x),
fract(y),
fract(z)
);
}
float vec3::dot(const vec3& left, const vec3& right)
{
return left.x*right.x + left.y*right.y + left.z*right.z;
}
float vec3::dot(const vec3& other)
{
return vec3::dot(*this, other);
}
vec3 vec3::cross(const vec3& left, const vec3& right)
{
return vec3(
left.y * right.z - left.z * right.y,
left.z * right.x - left.x * right.z,
left.x * right.y - left.y * right.x
);
}
vec3 vec3::cross(const vec3& other)
{
return vec3::cross(*this, other);
}
vec3 vec3::operator+(const vec3& other) const
{
return vec3(x + other.x, y + other.y, z + other.z);
}
void vec3::operator+=(const vec3& other)
{
*this = *this + other;
}
vec3 vec3::operator-(const vec3& other) const
{
return vec3(x - other.x, y - other.y, z - other.z);
}
void vec3::operator-=(const vec3& other)
{
*this = *this - other;
}
vec3 vec3::operator*(const vec3& other) const
{
return vec3(x * other.x, y * other.y, z * other.z);
}
void vec3::operator*=(const vec3& other)
{
*this = *this * other;
}
vec3 vec3::operator*(float factor) const
{
return vec3(x * factor, y * factor, z * factor);
}
void vec3::operator*=(float factor)
{
*this = *this * factor;
}
vec3 vec3::operator/(float factor) const
{
return vec3(x / factor, y / factor, z / factor);
}
void vec3::operator/=(float factor)
{
*this = *this / factor;
}
float vec3::operator[](int axis)
{
switch(axis)
{
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
return 0;
}
}
vec3 operator/(float val, const vec3& vec)
{
return vec3(
val / vec.x,
val / vec.y,
val / vec.z
);
}
// vec2
void vec2::zero()
{
x = 0; y = 0;
}
void vec2::normalize()
{
float magnitude = sqrt(x*x + y*y);
if(magnitude == 0)
return;
x /= magnitude;
y /= magnitude;
}
float vec2::dot(const vec2& left, const vec2& right)
{
return left.x*right.x + left.y*right.y;
}
float vec2::dot(const vec2& other)
{
return vec2::dot(*this, other);
}
vec2 vec2::operator+(const vec2& other) const
{
return vec2(x + other.x, y + other.y);
}
void vec2::operator+=(const vec2& other)
{
*this = *this + other;
}
vec2 vec2::operator-(const vec2& other) const
{
return vec2(x - other.x, y - other.y);
}
void vec2::operator-=(const vec2& other)
{
*this = *this - other;
}
vec2 vec2::operator*(float factor) const
{
return vec2(x * factor, y * factor);
}
void vec2::operator*=(float factor)
{
*this = *this * factor;
}
vec2 vec2::operator/(float factor) const
{
return vec2(x / factor, y / factor);
}
void vec2::operator/=(float factor)
{
*this = *this / factor;
}
vec2 vec2::operator/(const vec2& other) const
{
return vec2(x / other.x, y / other.y);
}
void vec2::operator/=(const vec2& other)
{
*this = *this / other;
}