-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.cpp
More file actions
127 lines (102 loc) · 2.69 KB
/
Copy pathvec3.cpp
File metadata and controls
127 lines (102 loc) · 2.69 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
#include "header/vec3.h"
#include <cmath>
#include <ostream>
vec3::vec3() {
vec[0] = 0;
vec[1] = 0;
vec[2] = 0;
}
vec3::vec3(double x, double y, double z) {
vec[0] = x;
vec[1] = y;
vec[2] = z;
}
double vec3::x() const { return vec[0]; }
double vec3::y() const { return vec[1]; }
double vec3::z() const { return vec[2]; }
vec3 vec3::operator-() const {
return vec3(-vec[0], -vec[1], -vec[2]);
}
double& vec3::operator[](int i) {
return vec[i];
}
vec3& vec3::operator+=(const vec3& other) {
vec[0] += other.vec[0];
vec[1] += other.vec[1];
vec[2] += other.vec[2];
return *this;
}
vec3& vec3::operator-=(const vec3& other) {
vec[0] -= other.vec[0];
vec[1] -= other.vec[1];
vec[2] -= other.vec[2];
return *this;
}
vec3& vec3::operator*=(const double scalar) {
vec[0] *= scalar;
vec[1] *= scalar;
vec[2] *= scalar;
return *this;
}
vec3& vec3::operator/=(const double scalar) {
vec[0] /= scalar;
vec[1] /= scalar;
vec[2] /= scalar;
return *this;
}
vec3& vec3::operator*=(const vec3& other) {
vec[0] *= other.vec[0];
vec[1] *= other.vec[1];
vec[2] *= other.vec[2];
return *this;
}
double vec3::dot(const vec3& other) const {
return vec[0]*other.vec[0] + vec[1]*other.vec[1] + vec[2]*other.vec[2];
}
vec3 vec3::cross(const vec3& other) const {
return vec3(
vec[1]*other.vec[2] - vec[2]*other.vec[1],
vec[2]*other.vec[0] - vec[0]*other.vec[2],
vec[0]*other.vec[1] - vec[1]*other.vec[0]
);
}
double vec3::length() const {
return std::sqrt(length_squared());
}
double vec3::length_squared() const {
return vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2];
}
vec3 vec3::unit_vector() const {
return *this / this->length();
}
vec3 lerp(vec3& a, vec3& b, double t) {
return vec3(
a.x() + (b.x() - a.x()) * t,
a.y() + (b.y() - a.y()) * t,
a.z() + (b.z() - a.z()) * t
);
}
std::ostream& operator<<(std::ostream& out, const vec3& v){
return out << v.x() << " " << v.y() << " " << v.z();
}
vec3 operator+(const vec3& u, const vec3& v) {
return vec3(u.x() + v.x(), u.y() + v.y(), u.z() + v.z());
}
vec3 operator+(const vec3& u, double other) {
return vec3(u.x() + other, u.y() + other, u.z() + other);
}
vec3 operator-(const vec3& u, const vec3& v) {
return vec3(u.x() - v.x(), u.y() - v.y(), u.z() - v.z());
}
vec3 operator*(const vec3& u, const vec3& v) {
return vec3(u.x() * v.x(), u.y() * v.y(), u.z() * v.z());
}
vec3 operator*(double t, const vec3& v) {
return vec3(t * v.x(), t * v.y(), t * v.z());
}
vec3 operator*(const vec3& v, double t) {
return t * v;
}
vec3 operator/(const vec3& v, double t) {
return (1 / t) * v;
}