-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3D.cpp
More file actions
184 lines (153 loc) · 3.51 KB
/
Copy pathVector3D.cpp
File metadata and controls
184 lines (153 loc) · 3.51 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Vector3D.h"
Vector3D::Vector3D() //constructor
{
x = 0;
y = 0;
z = 0;
}
Vector3D::Vector3D(float x1, float y1, float z1) //construct with values.
{
x = x1;
y = y1;
z = z1;
}
Vector3D::Vector3D(const Vector3D& vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
}
//addition
Vector3D Vector3D::operator+(const Vector3D& vec)
{
//Returns a new vector summing the values for each component with the
//corresponding component in the added vector
return Vector3D(x + vec.x, y + vec.y, z + vec.z);
}
Vector3D& Vector3D::operator+=(const Vector3D& vec)
{
//Returns ‘this’ pointer (i.e. self-reference summing the values for
//each component with the corresponding component in the added vector
x += vec.x;
y += vec.y;
z += vec.z;
return *this;
}
//substraction//
Vector3D Vector3D::operator-(const Vector3D& vec)
{
return Vector3D(x - vec.x, y - vec.y, z - vec.z);
}
Vector3D& Vector3D::operator-=(const Vector3D& vec)
{
//similar to addition
x -= vec.x;
y -= vec.y;
z -= vec.z;
return *this;
}
//scalar multiplication
Vector3D Vector3D::operator*(float value)
{
return Vector3D(x * value, y * value, z * value);
}
Vector3D& Vector3D::operator*=(float value)
{
//similar to subtraction
x *= value;
y *= value;
z *= value;
return *this;
}
//scalar division
Vector3D Vector3D::operator/(float value)
{
assert(value != 0); //prevent divide by 0
return Vector3D(x / value, y / value, z / value);
}
Vector3D& Vector3D::operator/=(float value)
{
assert(value != 0);
//similar to multiplication
x /= value;
y /= value;
z /= value;
return *this;
}
Vector3D& Vector3D::operator=(const Vector3D& vec)
{
x = vec.x;
y = vec.y;
z = vec.z;
return *this;
}
//Dot product
float Vector3D::dot_product(const Vector3D& vec)
{
//returns (x1*x2 + y1*y2 + x1*z2) where these are the terms from
// each vector
return x * vec.x + vec.y * y + vec.z * z;
}
//cross product
Vector3D Vector3D::cross_product(const Vector3D& vec)
{
//Calculate the terms (ni,nj,nk) using the dot product formula
//Then use to construct a vector using those terms and return
// as an example using vec to represent second vector
// the term ni in the output (new)vector is calculated as
float ni = y * vec.z - z * vec.y;
float nj = z * vec.x - x * vec.z;
float nk = x * vec.y - y * vec.x;
return Vector3D(ni, nj, nk);
}
float Vector3D::magnitude()
{
return sqrt(square());
}
//find magnitude of vector
float Vector3D::magnitude(const Vector3D& vec)
{
return (sqrtf(powf(vec.x, 2) + powf(vec.y, 2) + powf(vec.z, 2)));
}
Vector3D Vector3D::addScaledVector(const Vector3D& vector, float scale)
{
float x, y, z;
x += vector.x * scale;
y += vector.x * scale;
z += vector.x * scale;
return Vector3D(x, y, z);
}
float Vector3D::square()
{
return x * x + y * y + z * z;
}
Vector3D Vector3D::normalization(const Vector3D& vec)
{
Vector3D temp = vec;
float unitVector = sqrt((temp.x * temp.x) + (temp.y * temp.y) + (temp.z * temp.z));
temp.x = temp.x / unitVector;
temp.y = temp.y / unitVector;
temp.z = temp.z / unitVector;
return temp;
}
float Vector3D::distance(const Vector3D& vec)
{
Vector3D dist = *this - vec;
return dist.magnitude();
}
float Vector3D::show_X()
{
return x;
}
float Vector3D::show_Y()
{
return y;
}
float Vector3D::show_Z()
{
return z;
}
void Vector3D::disp()
{
cout << x << " " << y << " " << z << endl;
}