-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
162 lines (134 loc) · 2.68 KB
/
vector.hpp
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
154
155
156
157
158
159
160
161
162
#ifndef _vector_h
#define _vector_h
#include "color.hpp"
#include <iostream>
#include <math.h>
using namespace std;
class Vector {
public:
double x,y,z;
Vector() : x(0.0),y(0.0),z(0.0) {}
Vector(double x, double y, double z)
{
this->x = x; this->y = y; this->z = z;
}
Vector operator+ (const Vector& vec2)
{
Vector vec1(0.0,0.0,0.0);
vec1.x = this->x + vec2.x;
vec1.y = this->y + vec2.y;
vec1.z = this->z + vec2.z;
return vec1;
}
Vector operator- (const Vector& vec2)
{
Vector vec1(0.0,0.0,0.0);
vec1.x = this->x - vec2.x;
vec1.y = this->y - vec2.y;
vec1.z = this->z - vec2.z;
return vec1;
}
Vector& operator= (const Vector& vec)
{
this->x = vec.x;
this->y = vec.y;
this->z = vec.z;
return *this;
}
bool operator!= (const Vector& vec)
{
if (this->x == vec.x && this->y == vec.y && this->z == vec.z)
{
return true;
}
return false;
}
Vector operator/ (const double& k)
{
Vector v1(0.0,0.0,0.0);
v1.x = this->x/k;
v1.y = this->y/k;
v1.z = this->z/k;
return v1;
}
Vector operator* (const double& k)
{
Vector v1(0.0,0.0,0.0);
v1.x = this->x * k;
v1.y = this->y * k;
v1.z = this->z * k;
return v1;
}
Vector& operator *= (const double& k)
{
this->x *= k;
this->y *= k;
this->z *= k;
return *this;
}
// Vector operator * (const double& k, const Vector& vec1)
// {
// return Vector(
// k * vec1.x,
// k * vec1.y,
// k * vec1.z);
// }
Vector& operator /= (const double& k)
{
this->x /= k;
this->y /= k;
this->z /= k;
return *this;
}
Vector& operator += (const Vector& vec)
{
this->x += vec.x;
this->y += vec.y;
this->z += vec.z;
return *this;
}
Vector& operator -= (const Vector& vec)
{
this->x -= vec.x;
this->y -= vec.y;
this->z -= vec.z;
return *this;
}
double DotProduct (const Vector& vec1, const Vector& vec2)
{
return (vec1.x * vec2.x) + (vec1.y * vec2.y) + (vec1.z * vec2.z);
}
Vector CrossProduct (const Vector& vec1, const Vector& vec2)
{
return Vector(
(vec1.y * vec2.z) - (vec1.z * vec2.y),
(vec1.z * vec2.x) - (vec1.x * vec2.z),
(vec1.x * vec2.y) - (vec1.y * vec2.x)
);
}
const double mag_square()
{
return ((x*x) + (y*y) + (z*z));
}
const double mag()
{
return sqrt(mag_square());
}
const Vector unit_vector()
{
const double m = mag();
return Vector(x/m, y/m, z/m);
}
void display_vector()
{
cout << "[" << x << "," << y << "," << z << "]" << endl;
}
};
/* Ray struct */
struct Ray
{
Vector pos;
Vector dir;
Color col;
};
#endif