-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3d.h
233 lines (187 loc) · 5.12 KB
/
Vector3d.h
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
#ifndef VECTOR3D_H
#define VECTOR3D_H
#include <math.h>
#include <iostream>
using namespace std;
class Vector3
{
public:
/*
Conctructors
*/
Vector3(){}
Vector3(float i, float j, float k);
~Vector3() {}
float length();
Vector3 normalize();
void getinfo(); //shows the vector's values.
void setinfo();
/*
When I first started I made the operators call the methods
but realized this would make things very messy considering
all the operations possible on vectors.
Will fix this later.
*/
float Dotproduct(Vector3 &vector2)
{
//method used to calculate inner product of two vectors
float ans;
ans = (itsx*vector2.itsx) + (itsy*vector2.itsy) + (itsz*vector2.itsz);
return ans;
}
//operators
float operator | (Vector3 &vector2);
void operator +=(Vector3 &vector2);
Vector3 operator + (Vector3 &vector2);
void operator -= (Vector3 &vector2);
Vector3 operator - (Vector3 &vector2);
void operator *= (const float value);
Vector3 operator * (const float value);
/*
there is not an operator that exists in c++
That looks like x for the cross product, so I am using carrot.
*/
Vector3 operator ^(Vector3 &vector2);
private:
float itsx;
float itsy;
float itsz;
};
//These are functions for calculating the relative motion between two points
Vector3 velb(Vector3& ra, Vector3& rb, Vector3& vela, Vector3& velba, Vector3& angv);
Vector3 accelb(Vector3& ra, Vector3& rb, Vector3& accela, Vector3& accelba, Vector3& velba, Vector3& angv,Vector3& angaccel);
Vector3::Vector3(float i, float j, float k):
itsx(i), itsy(j), itsz(k)
{
//cout << "Vector constructor.\n";
}
float Vector3::length()
{
//returns scalar value of the vector's length
float answer;
answer = sqrt((pow(itsx,2) + pow(itsy,2) + pow(itsz, 2)));
return answer;
}
Vector3 Vector3::normalize()
{
/*
same idea as +,-,and ^ operators. It will
return a vector that is the unit vector of original.
Could have made an operator, but I feel this is more clear and
easier to work with.
*/
//intermediate length value
float veclength = this->length();
//Making intermediate values to make it more readable
float i = itsx/veclength;
float j = itsy/veclength;
float k = itsz/veclength;
return Vector3(i,j,k);
}
void Vector3::getinfo()
{
cout << "\nx: " << itsx;
cout << " y: " << itsy;
cout << " z: " << itsz << endl;
}
void Vector3::setinfo()
{
float x, y, z;
cout << "Please input the x coordinate." << endl;
cin >> x;
cout << "Please input the y coordinate." << endl;
cin >> y;
cout << "Please input the z coordinate." << endl;
cin >> z;
cout << endl;
itsx = x;
itsy = y;
itsz = z;
}
float Vector3::operator| (Vector3 &vector2)
{
/*
I selected this operator for dot product because
it resembles the notation used for an inner product space.
*/
return Vector3::Dotproduct(vector2);
}
void Vector3::operator+= (Vector3 & vector2)
{
/*
This is supposed to change the first vector in the
operator to be the sum of the two vectors
*/
itsx += vector2.itsx;
itsy += vector2.itsy;
itsz += vector2.itsz;
}
Vector3 Vector3::operator+ (Vector3 &vector2)
{
/*
returns a vector that holds the answer, so have to create a new vector
and initialize with (vector1+vector2).
*/
return Vector3(itsx + vector2.itsx, itsy + vector2.itsy, itsz + vector2.itsz);
}
void Vector3::operator-= (Vector3 &vector2)
{
//same idea as +=, but with subtraction.
itsx -= vector2.itsx;
itsy -= vector2.itsy;
itsz -= vector2.itsz;
}
Vector3 Vector3::operator - (Vector3 &vector2)
{
//same idea as +, but with subtraction.
return Vector3(itsx - vector2.itsx, itsy - vector2.itsy, itsz - vector2.itsz);
}
void Vector3::operator *= (const float value)
{
/*
This is element wise multiplication.
It will change the vector you are working with.
*/
itsx *= value;
itsy *= value;
itsz *= value;
}
Vector3 Vector3::operator* (const float value)
{
//element wise multiplication making a new vector.
return Vector3(value*itsx, value*itsy, value*itsz);
}
Vector3 Vector3::operator^ (Vector3 &vector2)
{
/*
This returns a vector that holds the cross product of
two vectors. Like the + and - operators. you need
to create a new vector object and initialize with the
operation.
*/
//to make it more readable I am making i,j,k values
float i = (itsy*vector2.itsz - itsz*vector2.itsy);
float j = - (itsx*vector2.itsz - itsz*vector2.itsx);
float k = (itsx*vector2.itsy - itsy*vector2.itsx);
return Vector3(i,j,k);
}
Vector3 velb(Vector3& ra, Vector3& rb, Vector3& vela, Vector3& velba, Vector3& angv)
{
//find rba, which is position of object b relative to a.
Vector3 rba(rb-ra);
//vector that holds the cross product of angular velocity and rba.
Vector3 angcrossrba(angv^rba);
return Vector3(vela+angcrossrba+velba);
}
Vector3 accelb(Vector3& ra, Vector3& rb, Vector3& accela, Vector3& accelba, Vector3& velba, Vector3& angv,Vector3& angaccel)
{
//relative position.
Vector3 rba(rb-ra);
Vector3 angaccelcrossrba(angaccel^rba);
Vector3 angvcrossrba(angv^rba);
Vector3 angvcrossagain(angv^angvcrossrba);
Vector3 doubleangv(angv*2);
Vector3 doubleangvcrossvelba(doubleangv^velba);
return Vector3(accela+angaccelcrossrba+angvcrossagain+doubleangvcrossvelba+accelba);
}
#endif