-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.cpp
74 lines (55 loc) · 1.78 KB
/
vector.cpp
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
// Simple vector class to avoid pulling in GLM as a dependency
#include "vector.h"
vector3::vector3() : x(0.0f), y(0.0f), z(0.0f) { }
vector3::vector3(float x, float y, float z) : x(x), y(y), z(z) { }
float vector3::operator*(const vector3& v) {
return this->x*v.x + this->y*v.y + this->z*v.z;
}
vector3 vector3::cross(const vector3& v) {
return vector3(this->y*v.z - this->z*v.y, this->z*v.x - this->x*v.z, this->x*v.y - this->y*v.z);
}
vector3 vector3::operator+(const vector3& v) {
return vector3(this->x + v.x, this->y + v.y, this->z + v.z);
}
vector3 vector3::operator-(const vector3& v) {
return vector3(this->x - v.x, this->y - v.y, this->z - v.z);
}
vector3 vector3::operator*(const float s) {
return vector3(this->x*s, this->y*s, this->z*s);
}
vector3& vector3::operator=(const vector3& v) {
this->x = v.x; this->y = v.y; this->z = v.z;
return *this;
}
float vector3::length() {
return sqrt(this->x*this->x + this->y*this->y + this->z*this->z);
}
vector3 vector3::normalize() {
float l = this->length();
return vector3(this->x/l, this->y/l, this->z/l);
}
vector2::vector2() : x(0.0f), y(0.0f) { }
vector2::vector2(float x, float y) : x(x), y(y) { }
float vector2::operator*(const vector2& v) {
return this->x*v.x + this->y*v.y;
}
vector2 vector2::operator+(const vector2& v) {
return vector2(this->x + v.x, this->y + v.y);
}
vector2 vector2::operator-(const vector2& v) {
return vector2(this->x - v.x, this->y - v.y);
}
vector2 vector2::operator*(const float s) {
return vector2(this->x*s, this->y*s);
}
vector2& vector2::operator=(const vector2& v) {
this->x = v.x; this->y = v.y;
return *this;
}
float vector2::length() {
return sqrt(this->x*this->x + this->y*this->y);
}
vector2 vector2::normalize() {
float l = this->length();
return vector2(this->x/l, this->y/l);
}