-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3dVector.cpp
More file actions
170 lines (125 loc) · 4.25 KB
/
Copy path3dVector.cpp
File metadata and controls
170 lines (125 loc) · 4.25 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
#include<iostream>
#include<math.h>
#include<assert.h>
using namespace std;
class vector3d {
private:
float x, y, z;
public:
vector3d() { //Constructor to instantiate and Initialise all vector components to 0
x = y = z = 0;
}
vector3d(float x1, float y1, float z1) { //Parameterised Constructor
x = x1;
y = y1;
z = z1;
}
vector3d(vector3d &vector) { //copy Constructor
x = vector.x;
y = vector.y;
z = vector.z;
}
vector3d operator+(const vector3d &operand); //returns the sum 2 vectors
vector3d &operator+=(const vector3d &operand); //Adds 2 vectors and stores the sum
vector3d operator-(const vector3d &operand); //Returns the differnce of 2 vectors
vector3d &operator-=(const vector3d &operand); //Subttracts 2 vectors and stores the difference
vector3d operator*(const float value); //Returns the product
vector3d &operator*=(const float value); //Multiplies and stores the product
vector3d operator/(const float value); //returns the quotient
vector3d &operator/=(const float value); //Divides and stores the quotient
float dot_product(const vector3d &vector); //returns the dot product
vector3d scalar_multiplication(const float scalar); //returns the product of vector and scalar
vector3d cross_product(const vector3d &vector); //returns the cross product of 2 vectors
float magnitude(); //returns the magnitude of the vector
vector3d normalisation(); //returns the normalised vector
float square(); //returns the square of the vector
float distance(const vector3d &vector); //returns the distance between 2 vectors
void display(); //prints the value of the vector
};
vector3d vector3d ::operator+(const vector3d &operand) {
vector3d result;
result.x = x + operand.x;
result.y = y + operand.y;
result.z = z + operand.z;
return result;
}
vector3d vector3d :: operator-(const vector3d &operand) {
vector3d result;
result.x = x - operand.x;
result.y = y - operand.y;
result.z = z - operand.z;
return result;
}
vector3d vector3d :: operator*(const float value) {
vector3d result;
result.x = x * value;
result.y = y * value;
result.z = z * value;
return result;
}
vector3d vector3d :: operator/(const float value) {
vector3d result;
result.x = x / value;
result.y = y / value;
result.z = z / value;
return result;
}
vector3d &vector3d :: operator+=(const vector3d &operand) {
x += operand.x;
y += operand.y;
z += operand.z;
return *this;
}
vector3d &vector3d :: operator-=(const vector3d &operand) {
x -= operand.x;
y -= operand.y;
z -= operand.z;
return *this;
}
vector3d &vector3d :: operator*=(const float value) {
x *= value;
y *= value;
z *= value;
return *this;
}
vector3d &vector3d :: operator/=(const float value) {
x /= value;
y /= value;
z /= value;
return *this;
}
float vector3d :: dot_product(const vector3d &vector) {
return x*vector.x + y*vector.y + z*vector.z;
}
vector3d vector3d :: scalar_multiplication(const float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
vector3d vector3d :: cross_product(const vector3d &vector) {
vector3d result;
result.x = y*vector.z - z*vector.y;
result.y = z*vector.x - x*vector.z;
result.z = x*vector.y - y*vector.x;
return result;
}
float vector3d :: magnitude() {
return sqrt(square());
}
float vector3d :: square() {
return x*x +y*y + z*z;
}
vector3d vector3d:: normalisation()
{
assert(magnitude() != 0);
*this /= magnitude();
return *this;
}
float vector3d :: distance(const vector3d &vector) {
vector3d dist(*this - vector);
return magnitude();
}
void vector3d :: display() {
cout << x << " " << y << " " << z << endl;
}