-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3.cpp
47 lines (36 loc) · 2.53 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
#include "Vec3.h"
Vector::Vector () {}
Vector::Vector (double n) : x_pos (n), y_pos (n), z_pos (n) {}
Vector::Vector (double x, double y, double z): x_pos (x), y_pos (y), z_pos (z) {}
double Vector::getX () { return this->x_pos; }
double Vector::getY () { return this->y_pos; }
double Vector::getZ () { return this->z_pos; }
Vector Vector::operator + (double &n) { return Vector (this->x_pos+n, this->y_pos+n, this->z_pos+n); }
Vector Vector::operator - (double &n) { return Vector (this->x_pos-n, this->y_pos-n, this->z_pos-n); }
Vector Vector::operator * (double &n) { return Vector (this->x_pos*n, this->y_pos*n, this->z_pos*n); }
Vector Vector::operator / (double &n) { return Vector (this->x_pos/n, this->y_pos/n, this->z_pos/n); }
Vector Vector::operator += (double &n) { return Vector (this->x_pos+=n, this->y_pos+=n, this->z_pos+=n); }
Vector Vector::operator + (Vector &vec) { return Vector (this->x_pos+vec.x_pos, this->y_pos+vec.y_pos, this->z_pos+vec.z_pos); }
Vector Vector::operator - (Vector &vec) { return Vector (this->x_pos-vec.x_pos, this->y_pos-vec.y_pos, this->z_pos-vec.z_pos); }
Vector Vector::operator * (Vector &vec) { return Vector (this->x_pos*vec.x_pos, this->y_pos*vec.y_pos, this->z_pos*vec.z_pos); }
Vector Vector::operator / (Vector &vec) { return Vector (this->x_pos/vec.x_pos, this->y_pos/vec.y_pos, this->z_pos/vec.z_pos); }
Vector Vector::operator += (Vector &vec) { return Vector (this->x_pos+=vec.x_pos, this->y_pos+=vec.y_pos, this->z_pos+=vec.z_pos); }
Vector Vector::negative () { return Vector (-this->x_pos, -this->y_pos, -this->z_pos); }
double Vector::dotProduct (Vector &vec) { return (this->x_pos*vec.x_pos +
this->y_pos*vec.y_pos +
this->z_pos*vec.z_pos); }
double Vector::magnitude () { return sqrt (this->x_pos*this->x_pos +
this->y_pos*this->y_pos +
this->z_pos*this->z_pos); }
Vector& Vector::normalize () {
double mag = magnitude ();
if (mag != 0) {
double inverse = 1/mag;
this->x_pos *= inverse;
this->y_pos *= inverse;
this->z_pos *= inverse;
}
return *this; }
Vector Vector::crossProduct (Vector &vec) { return Vector (this->y_pos*vec.z_pos - this->z_pos*vec.y_pos,
this->z_pos*vec.x_pos - this->x_pos*vec.z_pos,
this->x_pos*vec.y_pos - this->y_pos*vec.x_pos); }