-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp~
More file actions
94 lines (77 loc) · 1.86 KB
/
Vector.cpp~
File metadata and controls
94 lines (77 loc) · 1.86 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
#include "Vector.hpp"
#include <math.h>
// Vector class method definitions
//constructor with no input
Vector::Vector(){}
//constructor with directions
Vector::Vector(double x, double y, double z){
set(x,y,z);
}
//setting a vector into a direction
void Vector::set(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
}
//dot product of two vectors
double Vector::dot(Vector v){
double dp = x * v.x + y * v.y + z * v.z;
return dp;
}
//normalizing a vector to length of 1
void Vector::normalize(void) {
Vector result;
double length = sqrt( pow(x,2) + pow(y, 2) + pow(z , 2.0) );
this->set(x / length, y / length, z / length);
}
//adding two vectors
Vector Vector::add(Vector v){
Vector rv;
rv.x = x + v.x;
rv.y = y + v.y;
rv.z = z + v.z;
return rv;
}
Vector Vector::operator+(Vector v){return this->add(v);}
//substracting input vector from a vector
Vector Vector::subtract(Vector v){
Vector rv;
rv.x = x - v.x;
rv.y = y - v.y;
rv.z = z - v.z;
return rv;
}
Vector Vector::operator-(Vector v){return this->subtract(v);}
Vector Vector::mult(Vector v){
Vector rv;
rv.x = x * v.x;
rv.y = y * v.y;
rv.z = z * v.z;
return rv;
}
Vector Vector::operator*(Vector v){return this->mult(v);}
Vector Vector::scal_mult(double s){
Vector rv;
rv.x = x * s;
rv.y = y * s;
rv.z = z * s;
return rv;
}
Vector Vector::operator*(double s){return this->scal_mult(s);}
Vector Vector::scal_divide(double s){
Vector rv;
rv.x = x / s;
rv.y = y / s;
rv.z = z / s;
return rv;
}
Vector Vector::operator/(double s){return this->scal_divide(s);}
double Vector::sum_components(void){
return this->x + this->y + this->z;
}
double Vector::length(void){
double len;
len = pow(this->x,2) + pow(this->y,2) + pow(this->z,2);
len = sqrt(len);
return len;
}