-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.cpp
153 lines (124 loc) · 3.03 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
146
147
148
149
150
151
152
153
#ifndef _VEC3
#include "vec3.h" //includes vec3, CMATH and IOSTREAM
#endif
using namespace std;
std::ostream& operator<<(std::ostream& ost, VEC3 v){
ost << v.x << " " << v.y << " " << v.z;
return ost;
}
std::istream& operator>>(std::istream& ist, VEC3 v){
ist >> v.x >> v.y >> v.z;
return ist;
}
VEC3& VEC3::operator+=(const VEC3& v2){
x+=v2.x;
y+=v2.y;
z+=v2.z;
return *this;
}
VEC3& VEC3::operator-=(const VEC3& v2){
x+=v2.x;
y+=v2.y;
z+=v2.z;
return *this;
}
VEC3& VEC3::operator*=(const VEC3& v2){
x+=v2.x;
y+=v2.y;
z+=v2.z;
return *this;
}
VEC3& VEC3::operator/=(const VEC3& v2){
x+=v2.x;
y+=v2.y;
z+=v2.z;
return *this;
}
VEC3& VEC3::operator+=(float f2){
x+=f2;
y+=f2;
z+=f2;
return *this;
}
VEC3& VEC3::operator-=(float f2){
x-=f2;
y-=f2;
z-=f2;
return *this;
}
VEC3& VEC3::operator*=(float f2){
x*=f2;
y*=f2;
z*=f2;
return *this;
}
VEC3& VEC3::operator/=(float f2){
x/=f2;
y/=f2;
z/=f2;
return *this;
}
float dot(const VEC3& v1, const VEC3& v2){
float res;
res = v1.x*v2.x+ v1.y*v2.y+ v1.z*v2.z;
return res;
}
VEC3 cross(const VEC3& v1, const VEC3& v2){
return VEC3(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v2.z*v1.x, v2.y*v1.x - v1.y*v2.x);
}
VEC3 operator+(const VEC3& v1, const VEC3& v2){
return VEC3(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);
}
VEC3 operator-(const VEC3& v1, const VEC3& v2){
return VEC3(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z);
}
VEC3 operator*(const VEC3& v1, const VEC3& v2){
return VEC3(v1.x*v2.x,v1.y*v2.y,v1.z*v2.z);
}
VEC3 operator/(const VEC3& v1, const VEC3& v2){
return VEC3(v1.x/v2.x,v1.y/v2.y,v1.z/v2.z);
}
VEC3 operator+(const VEC3& v1, float f2){
return VEC3(v1.x+f2, v1.y+f2, v1.z+f2);
}
VEC3 operator-(const VEC3& v1, float f2){
return VEC3(v1.x-f2, v1.y-f2, v1.z-f2);
}
VEC3 operator*(const VEC3& v1, float f2){
return VEC3(v1.x*f2, v1.y*f2, v1.z*f2);
}
VEC3 operator/(const VEC3& v1, float f2){
return VEC3(v1.x/f2, v1.y/f2, v1.z/f2);
}
VEC3 operator+(float f2, const VEC3& v1){
return VEC3(v1.x+f2,v1.y+f2,v1.z+f2);
}
VEC3 operator*(float f2, const VEC3& v1){
return VEC3(v1.x*f2,v1.y*f2,v1.z*f2);
}
VEC3 operator-(float f2, const VEC3& v1){
return VEC3(f2-v1.x,f2-v1.y,f2-v1.z);
}
VEC3 operator/(float f2, const VEC3& v1){
return VEC3(f2/v1.x,f2/v1.y,f2/v1.z);
}
void VEC3::normalize(){
*this /= sqrt(x*x+y*y+z*z);
}
float VEC3::magnitude(){
return sqrt(x*x+y*y+z*z);
}
// int main(){
// VEC3 v1(1.0f,1.0f,2.0f);
// VEC3 v2(5.0f,2.0f,1.0f);
// cout << v1 <<std::endl << v2 << std::endl;
// // cout << v1+v2 << "\n" << v1*v2 << "\n" << v1*v2 << "\n" << v1-v2 << "\n" << v1/v2 << "\n";
// // cout << v1+2.0f << "\n" << v1*2.0f << "\n" << v1*2.0f << "\n" << v1-2.0f << "\n" << v1/2.0f << "\n";
// cout << -v1 << std::endl;
// v1.normalize();
// cout << v1 << std::endl;
// cout << v2.magnitude() << std::endl;
// v1+= v2;
// cout << v1 << std::endl;
// cout << dot(v1, v2);
// }