-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector3D.h
89 lines (72 loc) · 1.28 KB
/
Vector3D.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
#ifndef _VECTOR3D_H_
#define _VECTOR3D_H_
#include <cmath>
class Vector2D
{
public:
double x, y;
public:
Vector2D(){x = 0; y = 0;}
Vector2D(double x, double y){this->x = x; this->y = y;}
};
class Vector3D
{
public:
double x, y, z;
public:
Vector3D(){x = 0; y = 0; z = 0;}
Vector3D(double x, double y, double z){this->x = x; this->y = y; this->z = z;}
Vector3D operator+ (const Vector3D& v)
{
return Vector3D(x+v.x, y+v.y, z+v.z);
}
Vector3D operator- (const Vector3D& v)
{
return Vector3D(x-v.x, y-v.y, z-v.z);
}
double operator* (const Vector3D& v)
{
return x*v.x+y*v.y+z*v.z;
}
Vector3D operator* (double v)
{
return Vector3D(x*v, y*v, z*v);
}
Vector3D operator^ (const Vector3D& v)
{
return Vector3D(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
}
Vector3D operator/(double v)
{
return Vector3D(x/v, y/v, z/v);
}
Vector3D& operator+=(const Vector3D& v)
{
x += v.x; y += v.y; z += v.z;
return *this;
}
Vector3D& operator=(const Vector3D& v)
{
x = v.x; y = v.y; z = v.z;
return *this;
}
double length()
{
return sqrt(x*x+y*y+z*z);
}
void norm()
{
double len = length();
x /= len; y /= len; z /= len;
}
double& operator[](unsigned int i)
{
switch(i)
{
case 0: return x;
case 1: return y;
case 2: return z;
}
}
};
#endif