-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_basics.cpp
387 lines (337 loc) · 10.5 KB
/
vector_basics.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "vector_basics.h"
//==============================================================================
// POINT CLASS
//==============================================================================
// Handles keeping the x,y,z of a single point and integrates with vectors
//==============================================================================
Point::Point(){
this->x=0;this->y=0;this->z=0;
}
Point::Point(double x, double y, double z){
this->x = x;
this->y = y;
this->z = z;
}
//==============================================================================
// OPERATOR OVERLOADS
//==============================================================================
Point& Point::operator+=(const Vector& other){
this->x += other.x;
this->y += other.y;
this->z += other.z;
return *this;
}
Point& Point::operator+=(const Point& other){
this->x += other.x;
this->y += other.y;
this->z += other.z;
return *this;
}
Point Point::operator+(const Vector& other){
Point pp;
pp.x = this->x + other.x;
pp.y = this->y + other.y;
pp.z = this->z + other.z;
return pp;
}
Point Point::operator+(const Point& other){
Point pp;
pp.x = this->x + other.x;
pp.y = this->y + other.y;
pp.z = this->z + other.z;
return pp;
}
Point& Point::operator*=(const double scale){
this->x *= scale;
this->y *= scale;
this->z *= scale;
return *this;
}
Point Point::operator*(const double scale){
Point pp;
pp.x = this->x * scale;
pp.y = this->y * scale;
pp.z = this->z * scale;
return pp;
}
Point& Point::operator/=(const double scale){
this->x /= scale;
this->y /= scale;
this->z /= scale;
return *this;
}
Point Point::operator/(const double scale){
Point pp;
pp.x = this->x / scale;
pp.y = this->y / scale;
pp.z = this->z / scale;
return pp;
}
bool Point::operator==(const Point& other){
return
this->x == other.x &&
this->y == other.y &&
this->z == other.z;
}
bool Point::operator!=(const Point& other){
return !(this->operator==(other));
}
double& Point::operator[](const int index){
return this->data[index];
}
// Herron: translates given point by additon
Point Point::translatePoint(double x, double y, double z){
Point pnt;
pnt.x = x;
pnt.y = y;
pnt.z = z;
*this += pnt;
return *this;
}
// Herron: returns scaled point
Point Point::scalePoint(double scaleX, double scaleY, double scaleZ){
this->x *= scaleX;
this->y *= scaleY;
this->z *= scaleZ;
return *this;
}
// Herron: returns point rotated with matrix
Point Point::rotatePoint(double theta, bool x, bool y, bool z){
float phi = theta * M_PI / 180.0;
double tMat[4][4];
if(x){
// std::cout << "rotate x " << theta << std::endl;
tMat[0][0] = 1.0; tMat[0][1] = 0.0; tMat[0][2] = 0.0; tMat[0][3] = 0.0;
tMat[1][0] = 0.0; tMat[1][1] = cos(phi); tMat[1][2] = sin(phi); tMat[1][3] = 0.0;
tMat[2][0] = 0.0; tMat[2][1] = -sin(phi); tMat[2][2] = cos(phi); tMat[2][3] = 0.0;
tMat[3][0] = 0.0; tMat[3][1] = 0.0; tMat[3][2] = 0.0; tMat[3][3] = 1.0;
}
else if (y){
// std::cout << "rotate y " << theta << std::endl;
tMat[0][0] = cos(phi); tMat[0][1] = 0.0; tMat[0][2] =-sin(phi); tMat[0][3] = 0.0;
tMat[1][0] = 0.0; tMat[1][1] = 1.0; tMat[1][2] = 0.0; tMat[1][3] = 0.0;
tMat[2][0] = sin(phi); tMat[2][1] = 0.0; tMat[2][2] = cos(phi); tMat[2][3] = 0.0;
tMat[3][0] = 0.0; tMat[3][1] = 0.0; tMat[3][2] = 0.0; tMat[3][3] = 1.0;
}
else if (z){
// std::cout << "rotate z " << theta << std::endl;
tMat[0][0] = cos(phi); tMat[0][1] =-sin(phi); tMat[0][2] = 0.0; tMat[0][3] = 0.0;
tMat[1][0] = sin(phi); tMat[1][1] = cos(phi); tMat[1][2] = 0.0; tMat[1][3] = 0.0;
tMat[2][0] = 0.0; tMat[2][1] = 0.0; tMat[2][2] = 1.0; tMat[2][3] = 0.0;
tMat[3][0] = 0.0; tMat[3][1] = 0.0; tMat[3][2] = 0.0; tMat[3][3] = 1.0;
}
Point tmp;
//tmp->x = 0, tmp->y = 0, tmp->z = 0;
tmp.x = this->x*tMat[0][0] + this->y*tMat[0][1] + this->z*tMat[0][2];
tmp.y = this->x*tMat[1][0] + this->y*tMat[1][1] + this->z*tMat[1][2];
tmp.z = this->x*tMat[2][0] + this->y*tMat[2][1] + this->z*tMat[2][2];
return tmp;
}
//==============================================================================
// VECTOR CLASS
//==============================================================================
// Handles keeping the x,y,z of a vector, does basic vector math, and
// integrates with points a little
//==============================================================================
Vector::Vector(){
this->x = 0;
this->y = 0;
this->z = 0;
}
Vector::Vector(double x, double y, double z){
this->x = x;
this->y = y;
this->z = z;
}
Vector::Vector(Point& p1, Point& p2){ // p1 is the starting point and p2 is where we are going
this->x = p2.x - p1.x;
this->y = p2.y - p1.y;
this->z = p2.z - p1.z;
}
//==============================================================================
// BASIC VECTOR OPERATIONS
//==============================================================================
double Vector::magnitude(){
double temp = (this->x*this->x) + (this->y*this->y) + (this->z*this->z);
if( temp <= 0){
printf("FREAK OUT - THE SQRT IS GETTING A NEGATIVE NUMBER\n\t%lf\n",temp);
}
temp = sqrt(temp);
return temp;
}
double Vector::length(){
return this->magnitude();
}
double Vector::dot(Vector& other){
return
this->x * other.x +
this->y * other.y +
this->z * other.z;
}
double Vector::dotProduct(Vector& other){
this->dot(other);
}
Vector Vector::cross(const Vector& other){
Vector pp;
pp.x = (this->y*other.z) - (this->z*other.y);
pp.y = (this->z*other.x) - (this->x*other.z);
pp.z = (this->x*other.y) - (this->y*other.x);
return pp;
}
Vector Vector::crossProduct(Vector& other){
this->cross(other);
}
//==============================================================================
// OPERATOR OVERLOADS
//==============================================================================
Vector& Vector::operator+=(const Vector& other){
this->x += other.x;
this->y += other.y;
this->z += other.z;
return *this;
}
Vector Vector::operator+(const Vector& other){
Vector pp;
pp.x = this->x + other.x;
pp.y = this->y + other.y;
pp.z = this->z + other.z;
return pp;
}
Vector& Vector::operator-=(const Vector& other){
this->x -= other.x;
this->y -= other.y;
this->z -= other.z;
return *this;
}
Vector Vector::operator-(const Vector& other){
Vector pp;
pp.x = this->x - other.x;
pp.y = this->y - other.y;
pp.z = this->z - other.z;
return pp;
}
Vector& Vector::operator*=(const double scale){
this->x *= scale;
this->y *= scale;
this->z *= scale;
return *this;
}
Vector Vector::operator*(const double scale){
Vector pp;
pp.x = this->x * scale;
pp.y = this->y * scale;
pp.z = this->z * scale;
return pp;
}
Vector& Vector::operator/=(const double scale){
this->x /= scale;
this->y /= scale;
this->z /= scale;
return *this;
}
Vector Vector::operator/(const double scale){
Vector pp;
pp.x = this->x / scale;
pp.y = this->y / scale;
pp.z = this->z / scale;
return pp;
}
bool Vector::operator==(const Vector& other){
return
this->x == other.x &&
this->y == other.y &&
this->z == other.z;
}
bool Vector::operator!=(const Vector& other){
return !(this->operator==(other));
}
double& Vector::operator[](const int index){
return this->data[index];
}
//==============================================================================
// LINE SEGMENT CLASS
//==============================================================================
// Handles keeping the defining points of a line segment and also can check for
// an intersection
//==============================================================================
LineSeg::LineSeg(){
}
LineSeg::LineSeg(const Point& p1, const Point& p2){
this->p1=p1;
this->p2=p2;
}
LineSeg::LineSeg(Point p1, const Vector& v){
this->p1=p1;
this->p2=p1+v;
}
Point& LineSeg::operator[](const int& index){
if(index==1) return this->p2;
return this->p1; // default for any num not 1
}
bool LineSeg::intersect(LineSeg& l2){
double ua;
double ub;
LineSeg &l1=*this;
double a_top[2][2];
double b_top[2][2];
double denom[2][2];
a_top[0][0] = l2[0][0] - l1[0][0];
a_top[0][1] = -1* ( l2[1][0] - l2[0][0] ) ;
a_top[1][0] = l2[0][1] - l1[0][1];
a_top[1][1] = -1* ( l2[1][1] - l2[0][1] );
b_top[0][0] = l1[1][0] - l1[0][0];
b_top[0][1] = l2[0][0] - l1[0][0];
b_top[1][0] = l1[1][1] - l1[0][1];
b_top[1][1] = l2[0][1] - l1[0][1];
denom[0][0] = l1[1][0] - l1[0][0];
denom[0][1] = -1* ( l2[1][0] - l2[0][0] );
denom[1][0] = l1[1][1] - l1[0][1];
denom[1][1] = -1* ( l2[1][1] - l2[0][1] );
double bot_det = (denom[0][0]*denom[1][1]) - (denom[0][1]*denom[1][0]);
double a_top_det = (a_top[0][0]*a_top[1][1]) - (a_top[0][1]*a_top[1][0]);
double b_top_det = (b_top[0][0]*b_top[1][1]) - (b_top[0][1]*b_top[1][0]);
if(bot_det==0) return false; // the lines are parralel
ua=a_top_det/bot_det;
ub=b_top_det/bot_det;
//these are just roughly guessed numbers
//i was having trouble with floating points math
//the ends of lines were rarely returning as colliding
if(ua>0.000000000000005 && ua<0.999999999999995 && ub>0.000000000000005 && ub<0.999999999999995)
return true;
return false;
}
Point LineSeg::intersection(LineSeg& l2){
double ua;
double ub;
LineSeg &l1=*this;
double a_top[2][2];
double b_top[2][2];
double denom[2][2];
a_top[0][0] = l2[0][0] - l1[0][0];
a_top[0][1] = -1* ( l2[1][0] - l2[0][0] ) ;
a_top[1][0] = l2[0][1] - l1[0][1];
a_top[1][1] = -1* ( l2[1][1] - l2[0][1] );
b_top[0][0] = l1[1][0] - l1[0][0];
b_top[0][1] = l2[0][0] - l1[0][0];
b_top[1][0] = l1[1][1] - l1[0][1];
b_top[1][1] = l2[0][1] - l1[0][1];
denom[0][0] = l1[1][0] - l1[0][0];
denom[0][1] = -1* ( l2[1][0] - l2[0][0] );
denom[1][0] = l1[1][1] - l1[0][1];
denom[1][1] = -1* ( l2[1][1] - l2[0][1] );
double bot_det = (denom[0][0]*denom[1][1]) - (denom[0][1]*denom[1][0]);
double a_top_det = (a_top[0][0]*a_top[1][1]) - (a_top[0][1]*a_top[1][0]);
double b_top_det = (b_top[0][0]*b_top[1][1]) - (b_top[0][1]*b_top[1][0]);
if(bot_det==0) return l1.p1; // the lines are parralel - so this seems the most sensible out of a non-sense situation
//the above happens in 2 cases, the lines are concurent or they never intersect
ua=a_top_det/bot_det;
ub=b_top_det/bot_det;
double x = (l2[1][0] - l2[0][0]) * ub ; //the amount down this line segment that the intersection occured
x += l2[0][0] ; // the offset
double y = (l1[1][1] - l1[0][1]) * ua ; //the amount down this line segment that the intersection occured
y += l1[0][1] ; // the offset
return Point(x,y,0);
}
Vector LineSeg::direction(){
return Vector(p1,p2);
}