-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
111 lines (109 loc) · 2.64 KB
/
Vector.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
// Vectores
#include <iostream>
#include <cmath>
using namespace std;
//---------------------- class vector3D --------------------
class vector3D{
double v[3];
public:
void cargue(double x0, double y0, double z0);
void show(void);
// Funciones de salida de componentes
double x(void){return v[0];};
double y(void){return v[1];};
double z(void){return v[2];};
//Lectura de Elementos
double & operator[](int i){return v[i];};
// Operaciones vectoriales
vector3D operator= (vector3D v2);
vector3D operator+ (vector3D v2);
vector3D operator+=(vector3D v2);
vector3D operator- (vector3D v2);
vector3D operator-=(vector3D v2);
// Producto por escalar
vector3D operator* (double a);
vector3D operator*=(double a);
friend vector3D operator* (double a,vector3D v1);
// Division por escalar
vector3D operator/ (double a);
// Producto cruz
vector3D operator^ (vector3D v2);
// Producto punto
double operator* (vector3D v2);
// Norma
friend double norma2(vector3D v1);
friend double norma(vector3D v1);
};
// Metodos de la clase vector3D
void vector3D::cargue(double x0, double y0, double z0){
v[0]=x0; v[1]=y0; v[2]=z0;
}
void vector3D::show(void){
cout << "(" <<v[0]<< "," <<v[1]<< "," <<v[2]<< ")" << endl;
}
vector3D vector3D::operator=(vector3D v2){
for(int i=0;i<3;i++)
v[i] = v2.v[i];
return *this;
}
vector3D vector3D::operator+(vector3D v2){
vector3D total;
for(int i=0;i<3;i++)
total.v[i] = v[i] + v2.v[i];
return total;
}
vector3D vector3D::operator+=(vector3D v2){
*this = *this + v2;
return *this;
}
vector3D vector3D::operator*(double a){
vector3D total;
for(int i=0;i<3;i++)
total.v[i] = a*v[i];
return total;
}
vector3D vector3D::operator*=(double a){
*this = (*this)*a;
return *this;
}
vector3D vector3D::operator/(double a){
double inver = 1.0/a;
vector3D total;
for(int i=0;i<3;i++)
total.v[i] = inver*v[i];
return total;
}
vector3D vector3D::operator-(vector3D v2){
return *this + v2*(-1);
}
vector3D vector3D::operator-=(vector3D v2){
*this = *this - v2;
return *this;
}
double vector3D::operator*(vector3D v2){
double p=0;
for(int i=0;i<3;i++)
p += v[i]*v2.v[i];
return p;
}
vector3D vector3D::operator^(vector3D v2){
vector3D c;
c.v[0] = v[1]*v2.v[2]-v[2]*v2.v[1];
c.v[1] = v[2]*v2.v[0]-v[0]*v2.v[2];
c.v[2] = v[0]*v2.v[1]-v[1]*v2.v[0];
return c;
}
vector3D operator*(double a,vector3D v1){
vector3D total;
total = v1*a;
return total;
}
double norma2(vector3D v1){
double n=0;
for(int i=0;i<3;i++)
n += v1.v[i]*v1.v[i];
return n;
}
double norma(vector3D v1){
return sqrt(norma2(v1));
}