-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector3.h
129 lines (106 loc) · 3.18 KB
/
vector3.h
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
/*
* vector3.h
*
* Created on: 18/03/2016
* Author: Eduardo Gutiérrez
*
* This class implements a vector composed of three real (double type in C++) components (x,y,z). It
should provide, at least, public methods for:
• Set and get its value
• Get the value of its components individually
• Get its modulus
• Normalize
• Vector addition and substraction
• Product by a real number
• Scalar product
*/
#ifndef VECTOR3_H_
#define VECTOR3_H_
#include <math.h>
#include <iostream>
#include <GL/glut.h>
#include <jpeglib.h>
using namespace std;
namespace MyBilliards {
class vector3 {
private:
double x,y,z; // vector composed by three real components ( x,y,z)
public:
//Constructors
vector3();
vector3(double x, double y, double z);
//Destructor
virtual ~vector3();
// Set and get its value ( get value is obtained oveloading operator =
void setVector3(double x, double y, double z);
void setVector3(vector3 v);
//Obtain/Get the value of its components individually
double getVector3x() const;
void setVector3x(double vector3x);
double getVector3y() const;
void setVector3y(double vector3y);
double getVector3z() const;
void setVector3z(double vector3z);
//Get its modulus
double getModulus();
//Normalize
vector3 NormalizeVector3();
//Product by a real number
double mulNumberVector3( double n, vector3 v) const;
//Scalar product
double scalarProductVector3(vector3 v1, vector3 v2) const;
//Vector addition and substraction : Overload +.- operator to add, two vector3 objects.
vector3 operator+(const vector3& b)
{
vector3 result;
result.x = this->x + b.x;
result.y = this->y + b.y;
result.z = this->z + b.z;
return result;
}
vector3 operator-(const vector3& b)
{
vector3 result;
result.x = this->x - b.x;
result.y = this->y - b.y;
result.z = this->z - b.z;
return result;
}
//get value is obtained oveloading operator =
void operator=(const vector3 &v )
{
x = v.x;
y = v.y;
z = v.z;
}
//hint: operator*(const double& number, const vector3& vector) and operator*(const vector3& vector, const double& number) are not the same
friend vector3 operator* (const double& number, const vector3& vector)
{
vector3 result;
result.x = number*vector.x;
result.y = number*vector.y;
result.z = number*vector.z;
return result;
}
friend vector3 operator* (const vector3& vector, const double& number)
{
vector3 result;
result.x = number*vector.x;
result.y = number*vector.y;
result.z = number*vector.z;
return result;
}
//Scalar product: If a = a1i + a2j + a3k and b = b1i + b2j + b3k then
// a · b = a1*b1 + a2*b2 + a3*b3
friend double operator* (const vector3& vector1, const vector3& vector2)
{
double result;
result= (vector1.x)*(vector2.x)+(vector1.y)*(vector2.y) + (vector1.z)*(vector2.z);
return result;
}
// operadores no miembros: formateamos entrada y salida de elementos vector3
friend istream &operator>>(istream & in, vector3 & entry);
friend ostream &operator<<(ostream & out, const vector3 & entry);
};
} /* namespace MyBilliards */
#endif /* VECTOR3_H_ */