-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
115 lines (98 loc) · 2.61 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include <math.h> //sqrtf
// Class represents a standard vector (or point) in 3D space.
// The vector class provides all the necessary basic functionality
// for calculations with vectors.
struct Vector {
float x, y, z;
Vector() { /*blank on purpose*/ }
Vector(float x, float y, float z): x(x), y(y), z(z) {}
Vector(const Vector& rhs) {
x = rhs.x;
y = rhs.y;
z = rhs.z;
}
float lengthSqr() const { return x*x + y*y + z*z; }
float length() const { return sqrtf(x*x + y*y + z*z); }
Vector normalize() {
const float factor = 1.0f / sqrtf(x*x + y*y + z*z);
x *= factor;
y *= factor;
z *= factor;
return *this;
}
Vector operator - (const Vector& rhs) const { return Vector(x - rhs.x, y - rhs.y, z - rhs.z); }
Vector operator - () const { return Vector(-x, -y, -z); }
Vector operator + (const Vector& rhs) const { return Vector(x + rhs.x, y + rhs.y, z + rhs.z); }
Vector operator * (float scalar) const { return Vector(x * scalar, y * scalar, z * scalar); }
Vector operator / (float scalar) const {
scalar = 1.0f / scalar;
return Vector(x * scalar, y * scalar, z * scalar);
}
Vector& operator = (const Vector& rhs) {
x = rhs.x;
y = rhs.y;
z = rhs.z;
return *this;
}
Vector& operator += (const Vector& rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Vector& operator += (float scalar) {
x += scalar;
y += scalar;
z += scalar;
return *this;
}
Vector& operator -= (const Vector& rhs) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Vector& operator -= (float scalar) {
x -= scalar;
y -= scalar;
z -= scalar;
return *this;
}
Vector& operator /= (float scalar) {
scalar = 1.0f / scalar;
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
void set(float x, float y, float z) { this->x = x; this->y = y; this->z = z; }
float& operator[] (int index) {
switch (index) {
case 0: return x;
case 1: return y;
case 2: return z;
default: return x;
}
}
const float& operator[] (int index) const {
switch (index) {
case 0: return x;
case 1: return y;
case 2: return z;
default: return x;
}
}
friend Vector operator*(float lhs, const Vector & rhs) { return rhs * lhs; }
};
/// Returns the value of the dot product of vectors 'a' and 'b'
inline float dot(const Vector& a, const Vector& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
/// Returns the vector resulting from the cross product of vectors 'a' and 'b'
inline Vector cross(const Vector& a, const Vector& b) {
const float x = a.y * b.z - a.z * b.y;
const float y = a.x * b.z - a.z * b.x;
const float z = a.x * b.y - a.y * b.x;
return Vector(x, -y, z);
}