-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.cpp
executable file
·145 lines (125 loc) · 2.87 KB
/
vec3.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
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
#include "vec3.h"
#include <cmath>
#include <iostream>
using namespace std;
vec3::vec3()
{
zeros();
}
vec3::vec3(double x, double y, double z)
{
components[0] = x;
components[1] = y;
components[2] = z;
}
void vec3::print()
{
// Will print matlab syntax vector. Output will be like: [2.09, 5.3, 9.1];
cout << "[" << components[0] << ", " << components[1] << ", " << components[2] << "]" << endl;
}
void vec3::print(string name)
{
// Will print matlab syntax vector with a name. Output will be like: A = [2.09, 5.3, 9.1];
cout << name << " = ";
print();
}
vec3 vec3::cross(vec3 otherVector)
{
return vec3(y()*otherVector.z()-z()*otherVector.y(), z()*otherVector.x()-x()*otherVector.z(), x()*otherVector.y()-y()*otherVector.x());
}
double vec3::dot(vec3 otherVector)
{
return otherVector[0]*components[0] + otherVector[1]*components[1] + otherVector[2]*components[2];
}
void vec3::normalize()
{
double length = this->length();
if(length > 0) {
components[0] /= length;
components[1] /= length;
components[2] /= length;
}
}
vec3 vec3::normalized()
{
vec3 newVector = *this;
newVector.normalize();
return newVector;
}
double vec3::lengthSquared()
{
// Returns the square of the length (or norm) of the vector
return components[0]*components[0]+components[1]*components[1]+components[2]*components[2];
}
double vec3::length()
{
// Returns the length (or norm) of the vector
return sqrt(lengthSquared());
}
void vec3::zeros()
{
components[0] = 0;
components[1] = 0;
components[2] = 0;
}
vec3 &vec3::operator+=(double rhs)
{
components[0] += rhs;
components[1] += rhs;
components[2] += rhs;
return *this;
}
vec3 &vec3::operator+=(vec3 rhs)
{
components[0] += rhs[0];
components[1] += rhs[1];
components[2] += rhs[2];
return *this;
}
vec3 &vec3::operator*=(double rhs)
{
components[0] *= rhs;
components[1] *= rhs;
components[2] *= rhs;
return *this;
}
vec3 &vec3::operator*=(vec3 rhs)
{
components[0] *= rhs[0];
components[1] *= rhs[1];
components[2] *= rhs[2];
return *this;
}
vec3 &vec3::operator-=(double rhs)
{
components[0] -= rhs;
components[1] -= rhs;
components[2] -= rhs;
return *this;
}
vec3 &vec3::operator-=(vec3 rhs)
{
components[0] -= rhs[0];
components[1] -= rhs[1];
components[2] -= rhs[2];
return *this;
}
vec3 &vec3::operator/=(double rhs)
{
components[0] /= rhs;
components[1] /= rhs;
components[2] /= rhs;
return *this;
}
vec3 &vec3::operator/=(vec3 rhs)
{
components[0] /= rhs[0];
components[1] /= rhs[1];
components[2] /= rhs[2];
return *this;
}
std::ostream &operator<<(std::ostream &os, const vec3 &myVector) // Allows cout << myVector << endl;
{
os << "[" << myVector.x() << ", " << myVector.y() << ", " << myVector.z() << "];";
return os;
}