-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3D.cpp
More file actions
90 lines (70 loc) · 1.87 KB
/
Vector3D.cpp
File metadata and controls
90 lines (70 loc) · 1.87 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
/**
Vector: The vector class
@file vector3D.h
@author Adanna Obibuaku
@date 14/11/20
*/
#ifndef VECT_HEADER
#define VECT_HEADER
#include "Vector3D.h"
#endif
#include <cmath>
const float Vector3D::DEFAULT_X = 0;
const float Vector3D::DEFAULT_Y = 0;
const float Vector3D::DEFAULT_Z = 0;
Vector3D::Vector3D(float x, float y, float z):x(x),y(y),z(z){
}
Vector3D::Vector3D():x(DEFAULT_X),y(DEFAULT_Y),z(DEFAULT_Z){
}
float Vector3D::magnitude() {
return sqrt((x*x)+(y*y)+(z*z));
}
Vector3D Vector3D::unit() {
float mag = magnitude(); // When magnitude is 0
if (mag == 0) {
return Vector3D(0,0,0);
}
return Vector3D(x/mag,y/mag,z/mag);
}
Vector3D Vector3D::orthogonal(Vector3D rhs) {
return ((*this)%rhs).unit();
}
float Vector3D::getX() {
return x;
}
float Vector3D::getY() {
return y;
}
float Vector3D::getZ() {
return z;
}
Vector3D Vector3D::operator+ (Vector3D rhs) {
return Vector3D(x + rhs.getX(), y+rhs.getY(), z+rhs.getZ());
}
Vector3D Vector3D::operator- (Vector3D rhs) {
return Vector3D(x - rhs.getX(), y-rhs.getY(), z-rhs.getZ());
}
float Vector3D::operator* (Vector3D rhs) {
return x * rhs.getX() + y*rhs.getY() + z*rhs.getZ();
}
Vector3D Vector3D::operator% (Vector3D rhs) {
return Vector3D((y*rhs.getZ())-(z*rhs.getY()), (z*rhs.getX()-x*rhs.getZ()), (x*rhs.getY()-y*rhs.getX()));
}
Vector3D Vector3D::operator* (float scalar) {
return Vector3D(x*scalar, y*scalar, z*scalar);
}
Vector3D Vector3D::operator/ (float scalar) {
return Vector3D(x/scalar, y/scalar, z/scalar);
}
ostream& operator<< (ostream& ostream , Vector3D& v) {
ostream << "(" << v.getX() << "," << v.getY() << "," << v.getZ() << ")\n";
return ostream;
}
/*
int main() {
Vector3D v = Vector3D(10,4,-1);
Vector3D v1 = Vector3D(1, 1, 14);
Vector3D v3 = v-v1;
cout << v.orthogonal(v1) << "\n";
return 0;
}*/