-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.cpp
More file actions
129 lines (103 loc) · 2.03 KB
/
vector3.cpp
File metadata and controls
129 lines (103 loc) · 2.03 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
#include<stdlib.h>
#include<iostream>
#include"vector3.h"
using namespace std;
Vector3::Vector3() {
}
void Vector3::set(float nX, float nY, float nZ) {
x = nX;
y = nY;
z = nZ;
w = 0;
}
void Vector3::print() {
cout<<"("<<x<<", "<<y<<", "<<z<<")\n";
}
Vector3& Vector3::operator =(const Vector3& b) {
x = b.x;
y = b.x;
z = b.x;
w = b.x;
}
Vector3 Vector3::operator +(const Vector3& b) const {
Vector3 ret;
ret.x = x + b.x;
ret.y = y + b.y;
ret.z = z + b.z;
ret.w = w;
return ret;
}
Vector3 Vector3::operator *(const Vector3& b) const {
Vector3 ret;
ret.x = y*b.z - z*b.y;
ret.y = z*b.x - x*b.z;
ret.z = x*b.y - y*b.x;
ret.w = w;
return ret;
}
Vector3 Vector3::operator *(const float f) const {
Vector3 ret;
ret.x = x * f;
ret.x = y * f;
ret.x = z * f;
ret.w = w;
return ret;
}
Vector3 Vector3::operator *(const Matrix& m) const {
Vector3 ret;
float d;
for(int i=0; i<3; i++) {
d = m.data[0][i] * x + m.data[1][i] * y +
m.data[2][i] * z + m.data[3][i] * w;
switch(i)
{
case 0: ret.x = d;break;
case 1: ret.y = d;break;
case 2: ret.z = d;break;
}
}
ret.w = w;
return ret;
}
Vector3 Vector3::operator *(const Matrix* m) const{
return operator *(*m);
}
Vector3& Vector3::operator +=(const Vector3& b) {
x += b.x;
y += b.z;
z += b.z;
}
Vector3& Vector3::operator *=(const Vector3& b) {
float nX = y*b.z - z*b.y;
float nY = z*b.x - x*b.z;
z = x*b.y - y*b.x;
x = nX;
y = nY;
}
Vector3& Vector3::operator *=(const float f) {
x *= f;
y *= f;
z *= f;
}
Vector3& Vector3::operator *=(const Matrix& m) {
float nX, nY, nZ, d;
for(int i=0; i<3; i++) {
d = m.data[0][i] * x + m.data[1][i] * y +
m.data[2][i] * z + m.data[3][i] * w;
switch(i)
{
case 0: nX = d;break;
case 1: nY = d;break;
case 2: nZ = d;break;
}
}
x = nX;
y = nY;
z = nZ;
}
Vector3& Vector3::operator *=(const Matrix* m) {
operator *=(*m);
}
float Vector3::dot(const Vector3& b) {
return x*b.x + y*b.y + z*b.z;
}