-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment2_Finaldraft.cpp
More file actions
284 lines (276 loc) · 11.9 KB
/
assignment2_Finaldraft.cpp
File metadata and controls
284 lines (276 loc) · 11.9 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
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
#include <bits/stdc++.h>
using namespace std;
typedef float ft;
class Vector_Generator {
public:
float x;
float y;
float z;
float a;
Vector_Generator(ft x, ft y, ft z, ft a) {
this->x = x;
this->y = y;
this->z = z;
this->a = a;
}
};
class Vector3D {
float x;
float y;
float z;
public:
Vector3D(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
Vector3D scalar_multiplication(float s) {
return Vector3D(this->x * s, this->y * s, this->z * s);
}
Vector3D scalar_division(float s) {
return Vector3D(this->x / s, this->y / s, this->z / s);
}
Vector3D vector_addition(Vector3D vector2) {
return Vector3D(this->x + vector2.x, this->y + vector2.y, this->z + vector2.z);
}
Vector3D vector_subtraction(Vector3D vector2) {
return Vector3D(this->x - vector2.x, this->y - vector2.y, this->z - vector2.z);
}
ft dot_product(Vector3D vector2) {
return (this->x * vector2.x + this->y * vector2.y + this->z * vector2.z);
}
Vector3D vector_product(Vector3D vector2) {
return Vector3D(this->y * vector2.z - this->z * vector2.y, this->z * vector2.x - this->x * vector2.z, this->x * vector2.y - this->y * vector2.x);
}
Vector3D reverse_direction(void) {
return Vector3D(-1 * this->x, -1 * this->y, -1 * this->z);
}
Vector3D component_multiplication(Vector3D vector2) {
return Vector3D(this->x * vector2.x, this->y * vector2.y, this->z * vector2.z);
}
ft modulus(void) {
return sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
}
ft scalar_triple_product(Vector3D vector2, Vector3D vector3) {
return ((vector2.y * vector3.z - vector2.z * vector3.y) * this->x + (vector2.z * vector3.x - vector2.x * vector3.z) * this->y + (vector2.x * vector3.y - vector2.y * vector3.x) * this->z);
}
Vector3D unit_vector() {
float mod = sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
return Vector3D(this->x / mod, this->y / mod, this->z / mod);
}
Vector_Generator vector_generator() {
float mod = sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
return Vector_Generator(mod, 57.3248408 * acos(this->x / mod), 57.3248408 * acos(this->y / mod), 57.3248408 * acos(this->z / mod));
}
ft angle_vectors(Vector3D vector2) {
return (57.3248408 * acos((this->x * vector2.x + this->y * vector2.y + this->z * vector2.z) /vector2.modulus() * sqrt(this->x * this->x + this->y * this->y + this->z * this->z)));
}
Vector3D intersectionof_vectorandplanes(Vector_Generator vector2) { // assuming that equation of plane is xx+yy+zz=a
return Vector3D(this->x * vector2.a / (vector2.x * this->x + vector2.y * this->y + vector2.z * this->z), this->y * vector2.a / (vector2.x * this->x + vector2.y * this->y + vector2.z * this->z), this->z * vector2.a / (vector2.x * this->x + vector2.y * this->y + vector2.z * this->z));
}
Vector3D vector_triple_product(Vector3D X, Vector3D Y) { // assuming vector product of form x*(x1*y1);
return Vector3D(dot_product(Y) * X.x - dot_product(X) * Y.x, dot_product(Y) * X.y - dot_product(X) * Y.y, dot_product(Y) * X.z - dot_product(X) * Y.z);
}
Vector3D x_rotation(ft angle) { // angle should be in radians;
return Vector3D(this->x, this->y * cos(angle) - this->z * sin(angle), this->y * sin(angle) + this->z * cos(angle));
}
Vector3D y_rotation(ft angle) {
return Vector3D(this->x * cos(angle) + this->z * sin(angle), this->y, this->z * cos(angle) - this->x * sin(angle));
}
Vector3D z_rotation(ft angle) {
return Vector3D(this->x * cos(angle) - this->y * sin(angle), this->x * sin(angle) + this->y * cos(angle), this->z);
}
void print(void) {
cout << "Answer is : " << this->x << "," << this->y << "," << this->z << endl;
}
// Operator Overloading
Vector3D operator+(Vector3D vector2) {
return Vector3D(this->x + vector2.x, this->y + vector2.y, this->z + vector2.z);
}
Vector3D operator-(Vector3D vector2) {
return Vector3D(this->x - vector2.x, this->y - vector2.y, this->z - vector2.z);
}
Vector3D operator*(Vector3D vector2) {
return Vector3D(this->x * vector2.x, this->y * vector2.y, this->z * vector2.z);
}
Vector3D projection_along_vector(Vector3D vector2) {
ft angle = angle_vectors(vector2);
return Vector3D(cos(angle) * this->x, cos(angle) * this->y, this->z * cos(angle));
}
Vector3D projection_perpendicular_vector(Vector3D vector2) {
ft angle = angle_vectors(vector2);
return Vector3D(sin(angle) * this->x, sin(angle) * this->y, this->z * sin(angle));
}
};
Vector3D readVector() {
float x, y, z;
char comma;
cin >> x >> comma >> y >> comma >> z;
return Vector3D(x, y, z);
}
int main() {
int a;
float s, angle;
cout << "What do you want to do ?" << endl << "Enter corresponding serial number corresponding to operation you want to perform." << endl;
cout << "1. Scalar Multiplication" << endl << "2. Scalar Division\n3. Vector Addition \n4. Vector Subtraction \n5. Dot Product" << endl;
cout << "6. Vector Product\n7. Reverse Direction\n8. Component Multiplication\n9. Modulus\n10. Scalar Triple Product\n11. Unit Vector" << endl;
cout << "12. Polar Representation of Vector\n13. Angle between vectors\n14. Intersection point of vector and plane\n15. Vector Triple Product" << endl;
cout << "16. Rotation about X axis\n17. Rotation about Y axis\n18. Rotation about Z axis" << endl;
cin >> a;
Vector3D vector1(0, 0, 0);
Vector3D vector2(0, 0, 0);
Vector3D vector3(0, 0, 0);
Vector3D result(0, 0, 0);
float result_scalar;
switch(a) {
case 1:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the scalar value: ";
cin >> s;
result = vector1.scalar_multiplication(s);
result.print();
break;
case 2:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the scalar value: ";
cin >> s;
result = vector1.scalar_division(s);
result.print();
break;
case 3:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
result = vector1.vector_addition(vector2);
result.print();
break;
case 4:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
result = vector1.vector_subtraction(vector2);
result.print();
break;
case 5:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
result_scalar = vector1.dot_product(vector2);
cout << "Dot Product is : " << result_scalar << endl;
break;
case 6:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
result = vector1.vector_product(vector2);
result.print();
break;
case 7:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
result = vector1.reverse_direction();
result.print();
break;
case 8:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
result = vector1.component_multiplication(vector2);
result.print();
break;
case 9:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
result_scalar = vector1.modulus();
cout << "Modulus is : " << result_scalar << endl;
break;
case 10:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
cout << "Enter the components of the third vector (x, y, z): ";
vector3 = readVector();
result_scalar = vector1.scalar_triple_product(vector2, vector3);
cout << "Scalar Triple Product is : " << result_scalar << endl;
break;
case 11:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
result = vector1.unit_vector();
result.print();
break;
case 12: {
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
Vector_Generator vg = vector1.vector_generator();
cout << "Polar Representation is : " << vg.x << ", " << vg.y << ", " << vg.z << ", " << vg.a << endl;
break;
}
case 13:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
result_scalar = vector1.angle_vectors(vector2);
cout << "Angle between vectors is : " << result_scalar << " degrees" << endl;
break;
case 14: {
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components and constant of the plane (a, b, c, d): ";
float a, b, c, d;
char comma;
cin >> a >> comma >> b >> comma >> c >> comma >> d;
Vector_Generator vg = Vector_Generator(a, b, c, d);
result = vector1.intersectionof_vectorandplanes(vg);
result.print();
break;
}
case 15:
cout << "Enter the components of the first vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the components of the second vector (x, y, z): ";
vector2 = readVector();
cout << "Enter the components of the third vector (x, y, z): ";
vector3 = readVector();
result = vector1.vector_triple_product(vector2, vector3);
result.print();
break;
case 16:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the angle in radians: ";
cin >> angle;
result = vector1.x_rotation(angle);
result.print();
break;
case 17:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the angle in radians: ";
cin >> angle;
result = vector1.y_rotation(angle);
result.print();
break;
case 18:
cout << "Enter the components of the vector (x, y, z): ";
vector1 = readVector();
cout << "Enter the angle in radians: ";
cin >> angle;
result = vector1.z_rotation(angle);
result.print();
break;
default:
cout << "Invalid choice!" << endl;
break;
}
return 0;
}