-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.cpp
124 lines (105 loc) · 2.18 KB
/
vector.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
#include "vector.h"
Vector::Vector() {
x = 0;
y = 0;
z = 0;
w = 0;
}
Vector::Vector(int ix, int iy, int iz) {
x = (float) ix;
y = (float) iy;
z = (float) iz;
w = 0.0f;
}
Vector::Vector(float ix, float iy, float iz) {
x = ix;
y = iy;
z = iz;
w = 0.0f;
}
Vector::Vector(float ix, float iy, float iz, float iw) {
x = ix;
y = iy;
z = iz;
w = iw;
}
void Vector::add(Vector v1, Vector v2) {
x = v1.x + v2.x;
y = v1.y + v2.y;
z = v1.z + v2.z;
}
void Vector::subtract(Vector v1, Vector v2) {
x = v1.x - v2.x;
y = v1.y - v2.y;
z = v1.z - v2.z;
}
void Vector::scalar_multiply(Vector v, float scalar) {
x = v.x * scalar;
y = v.y * scalar;
z = v.z * scalar;
}
void Vector::scalar_divide(Vector v, float scalar) {
x = v.x / scalar;
y = v.y / scalar;
z = v.z / scalar;
}
Vector Vector::operator+(const Vector& v) {
Vector result;
result.x = this->x + v.x;
result.y = this->y + v.y;
result.z = this->z + v.z;
return result;
}
Vector Vector::operator-(const Vector& v) {
Vector result;
result.x = this->x - v.x;
result.y = this->y - v.y;
result.z = this->z - v.z;
return result;
}
Vector Vector::operator*(const Vector& v) {
Vector result;
result.x = this->x * v.x;
result.y = this->y * v.y;
result.z = this->z * v.z;
return result;
}
Vector Vector::operator*(float scalar) {
Vector result;
result.x = this->x * scalar;
result.y = this->y * scalar;
result.z = this->z * scalar;
return result;
}
Vector Vector::operator/(const Vector& v) {
Vector result;
result.x = this->x / v.x;
result.y = this->y / v.y;
result.z = this->z / v.z;
return result;
}
Vector Vector::operator/(float scalar) {
Vector result;
result.x = this->x / scalar;
result.y = this->y / scalar;
result.z = this->z / scalar;
return result;
}
void Vector::normalize() {
float magnitude = pow(x, 2) + pow(y, 2) + pow(z, 2);
magnitude = sqrt(magnitude);
if( magnitude > 0) {
x = x / magnitude;
y = y / magnitude;
z = z / magnitude;
}
}
float Vector::dot_product(Vector v) {
return x * v.x + y * v.y + z * v.z;
}
void Vector::print() {
cout << "<" << x << ", " << y << ", " << z << " ," << w << ">";
}
float dot_product(Vector v1, Vector v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}