-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector3.cpp
148 lines (114 loc) · 2.88 KB
/
vector3.cpp
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
/*
* Pacman.cpp
*
* Created on: 18/03/2016
* Author: Eduardo Gutiérrez
*
*/
#include "vector3.h"
namespace MyBilliards {
vector3::vector3() {
// TODO Auto-generated constructor stub
x=0;
y=0;
z=0;
}
vector3::vector3(double xaxis, double yaxis, double zaxis)
{
x=xaxis;
y=yaxis;
z=zaxis;
}
vector3::~vector3() {
// TODO Auto-generated destructor stub
}
//El get lo obtengo sobrecargando el operador =
void vector3::setVector3(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
void vector3::setVector3(vector3 v){
this->x = v.x;
this->y = v.y;
this->z = v.z;
}
//Get the value of its components individually
double vector3::getVector3x() const
{
return x;
}
void vector3::setVector3x(double vector3x)
{
this->x = vector3x;
}
double vector3::getVector3y() const
{
return y;
}
void vector3::setVector3y(double vector3y)
{
this->y = vector3y;
}
double vector3::getVector3z() const
{
return z;
}
void vector3::setVector3z(double vector3z)
{
this->z = vector3z;
}
//Get its modulus
/*The magnitude (length) of the vector is,
length = sqrt((ax * ax) + (ay * ay) + (az * az))
length = sqrt(9 + 1 + 4) = 3.742
As a short-hand notation the magnitude of a vector is written with two vertical lines,
|a| = sqrt((ax * ax) + (ay * ay) + (az * az))*/
double vector3::getModulus()
{
return sqrt( (x*x) + (y*y)+ (z*z));
}
/*Normalize
//Normalizing a vector involves two steps:
1 calculate its length, then,
2 divide each of its (xy or xyz) components by its length.
Given vector a its xyz components are calculated as follows,
x = ax/|a|
y = ay/|a|
z = az/|a|
As a "worked example" the vector shown in figure 1 has the xyz components of 3, 1, 2 and a length of 3.742. Therefore, a normalized copy of the vector will have components,
x = 3.0 / 3.742 = 0.802
y = 1.0 / 3.742 = 0.267
z = 2.0 / 3.742 = 0.534
*/
vector3 vector3::NormalizeVector3()
{
double length= sqrt( (x*x) + (y*y)+ (z*z));
vector3 normalizeVector;
if ( length!=0)
{
//cout << "X a normalizar" << x;
//cout << "length a normalizar" << length;
normalizeVector.x = x/length;
//cout << "normalize x" << normalizeVector.x;
normalizeVector.y = y/length;
normalizeVector.z = z/length;
}
else cout << "Imposible calcular vector Normalizado, su longitud es 0";
return normalizeVector;
}
//Vector addition and substraction
// Sobrecarga del operador + para la clase vector3
// Sobrecarga del operador - para la clase vector3
//Scalar product -> Obtained by overloading *
//Implemetaci¢n de operadores no miembros---------------------------------------------------------------------
istream& operator>>(istream & in, vector3 & entry)
{
return in >> entry.x >> entry.y >> entry.z;
}
ostream& operator<<(ostream & out, const vector3 & entry)
{
return out << "[ x: " << entry.x << " , y: " << entry.y << " , z: " << entry.z << " ]";
}
} /* namespace MyBilliards */