-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
103 lines (96 loc) · 1.94 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
#include <math.h>
#include <iostream>
class Vector3
{
public:
double x,y,z;
Vector3( double x=0.0, double y=0.0, double z=0.0 ){
Vector3::x = x;
Vector3::y = y;
Vector3::z = z;
}
double mag(void){
return sqrt(x*x + y*y + z*z);
}
double normalize( void ){
double m = mag();
if(m==0.0){
x=1.0;
y=0.0;
z=0.0;
}else{
*this/=m;
}
return m;
}
void print(const char* p_str){
std::cout << p_str << "\n";
std::cout << "x:" << x << " y:" << y << " z:" << z << "\n";
}
Vector3& operator=(const Vector3& a){
x = a.x;
y = a.y;
z = a.z;
return *this;
}
Vector3 operator+(void){
return *this;
}
Vector3 operator-(void){
Vector3 l_r(-x,-y,-z);
return l_r;
}
Vector3& operator/=(const double a){
x/=a;
y/=a;
z/=a;
return *this;
}
Vector3& operator+=(const Vector3& a){
x += a.x;
y += a.y;
z += a.z;
return *this;
}
Vector3& operator-=(const Vector3& a){
x -= a.x;
y -= a.y;
z -= a.z;
return *this;
}
friend Vector3 operator+(const Vector3& a, const Vector3& b);
friend Vector3 operator-(const Vector3& a, const Vector3& b);
friend double operator*(const Vector3& a, const Vector3& b);
friend Vector3 operator*(double a, const Vector3& b);
friend Vector3 operator*(const Vector3& a, double b);
friend Vector3 operator^(const Vector3& a, const Vector3& b);
};
Vector3 operator+(const Vector3& a,const Vector3& b)
{
Vector3 l_r(a.x+b.x,a.y+b.y,a.z+b.z);
return l_r;
}
Vector3 operator-(const Vector3& a, const Vector3& b)
{
Vector3 l_r(a.x-b.x,a.y-b.y,a.z-b.z);
return l_r;
}
double operator*(const Vector3& a, const Vector3& b)
{
return a.x*b.x + a.y*b.y + a.z*b.z;
}
Vector3 operator*(double a, const Vector3& b)
{
Vector3 l_r(b.x*a,b.y*a,b.z*a);
return l_r;
}
Vector3 operator*(const Vector3& a, double b)
{
Vector3 l_r(a.x*b,a.y*b,a.z*b);
return l_r;
}
Vector3 operator^(const Vector3& a, const Vector3& b)
{
Vector3 l_r(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);
return l_r;
}