-
Notifications
You must be signed in to change notification settings - Fork 12
/
vec3.h
109 lines (88 loc) · 2.57 KB
/
vec3.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
#pragma once
#include <string>
#include <vector>
class vec3 {
private:
double components[3];
public:
vec3();
vec3(double x, double y, double z);
double lengthSquared();
double length();
// Actions
void zeros();
vec3 cross(vec3 otherVector);
double dot(vec3 otherVector);
void normalize();
vec3 normalized();
// Getters and setters
double x() const { return components[0]; }
double y() const { return components[1]; }
double z() const { return components[2]; }
void setX(double x) { components[0] = x; }
void setY(double y) { components[1] = y; }
void setZ(double z) { components[2] = z; }
// Convenience functions
void print();
void print(std::string name);
friend std::ostream& operator<<(std::ostream& os, const vec3& vec);
// Operators
double operator()(int index) const { return components[index]; } // Allows access like myVector(0)
double& operator[](int index) { return components[index]; } // Allows access like myVector[0]
vec3& operator+=(double rhs); // Componentwise addition with scalar
vec3& operator+=(const vec3& rhs); // Componentwise addition with vector
vec3& operator*=(double rhs); // Componentwise multiplication with scalar
vec3& operator*=(const vec3& rhs); // Componentwise multiplicationwith vector
vec3& operator-=(double rhs); // Componentwise subtraction with scalar
vec3& operator-=(const vec3& rhs); // Componentwise subtraction with vector
vec3& operator/=(double rhs); // Componentwise division with scalar
vec3& operator/=(const vec3& rhs); // Componentwise division with vector
};
inline vec3 operator+(vec3 lhs, double rhs) {
lhs += rhs;
return lhs;
}
inline vec3 operator+(double lhs, vec3 rhs) {
rhs += lhs;
return rhs;
}
inline vec3 operator+(vec3 lhs, vec3 rhs) {
lhs += rhs;
return lhs;
}
inline vec3 operator-(vec3 lhs, double rhs) {
lhs -= rhs;
return lhs;
}
inline vec3 operator-(double lhs, vec3 rhs) {
rhs -= lhs;
return rhs;
}
inline vec3 operator-(vec3 lhs, vec3 rhs) {
lhs -= rhs;
return lhs;
}
inline vec3 operator*(vec3 lhs, double rhs) {
lhs *= rhs;
return lhs;
}
inline vec3 operator*(double lhs, vec3 rhs) {
rhs *= lhs;
return rhs;
}
inline vec3 operator*(vec3 lhs, vec3 rhs) {
lhs *= rhs;
return lhs;
}
inline vec3 operator/(vec3 lhs, double rhs) {
lhs /= rhs;
return lhs;
}
inline vec3 operator/(double lhs, vec3 rhs) {
rhs /= lhs;
return rhs;
}
inline vec3 operator/(vec3 lhs, vec3 rhs) {
lhs /= rhs;
return lhs;
}