forked from martinjrobins/SPH-DEM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector.h
executable file
·145 lines (100 loc) · 3.79 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
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
#include <iostream>
#include <math.h>
class Vector2D {
public:
real x,y; // vector components
Vector2D() { x=y=0; } // constructor
Vector2D(const real pX, const real pY)
: x(pX), y(pY) {}
// vector arithmatic
Vector2D operator+(const Vector2D pV) const
{ Vector2D out( x+pV.x, y+pV.y); return out; }
Vector2D operator-(const Vector2D pV) const
{ Vector2D out( x-pV.x, y-pV.y); return out; }
Vector2D& operator+=(const Vector2D pV)
{ x+=pV.x; y+=pV.y; return *this; }
Vector2D& operator-=(const Vector2D pV)
{ x-=pV.x; y-=pV.y; return *this; }
real dot(const Vector2D pV) const // dot product
{ return x*pV.x + y*pV.y; }
Vector2D operator*(const real pR) const // * a scalar
{ Vector2D out( x*pR, y*pR); return out; }
friend Vector2D operator*(const real pR, const Vector2D pV)
{ Vector2D out( pV.x*pR, pV.y*pR); return out; }
Vector2D& operator*=(const real pR)
{ x*=pR; y*=pR; return *this; }
Vector2D& operator=(const real pR)
{ x=pR; y=pR; return *this; }
// magnitude
friend real len2(const Vector2D pV)
{ return pV.x*pV.x+pV.y*pV.y;}
friend real len(const Vector2D pV)
{ return sqrt(len2(pV)); }
// comparison
int operator==( const Vector2D pV) const
{ return x==pV.x && y==pV.y; }
int operator!=( const Vector2D pV) const
{ return x!=pV.x || y!=pV.y; }
int operator<=( const Vector2D pV) const
{ return x<=pV.x && y<=pV.y; }
int operator>=( const Vector2D pV) const
{ return x>=pV.x && y>=pV.y; }
int operator<( const Vector2D pV) const
{ return x<pV.x && y<pV.y; }
int operator>( const Vector2D pV) const
{ return x>pV.x && y>pV.y; }
// input/output
//friend ostream& operator<<(ostream& pStr, const Vector2D& pV)
//{ return (pStr << '(' << pV.x << ',' << pV.y << ')'); }
//friend istream& operator>>(istream pStr, Vector2D& pV)
//{ return (pStr >> pV.x >> pV.y); }
};
class Vector3D {
public:
real x,y,z; // vector components
Vector3D() { x=y=z=0; } // constructor
Vector3D(const real pX, const real pY, const real pZ)
: x(pX), y(pY), z(pZ) {}
// vector arithmatic
Vector3D operator+(const Vector3D pV) const
{ Vector3D out( x+pV.x, y+pV.y, z+pV.z ); return out; }
Vector3D operator-(const Vector3D pV) const
{ Vector3D out( x-pV.x, y-pV.y, z-pV.z ); return out; }
Vector3D& operator+=(const Vector3D pV)
{ x+=pV.x; y+=pV.y; z+=pV.z; return *this; }
Vector3D& operator-=(const Vector3D pV)
{ x-=pV.x; y-=pV.y; z-=pV.z; return *this; }
real dot(const Vector3D pV) const // dot product
{ return x*pV.x + y*pV.y + z*pV.z; }
Vector3D operator*(const real pR) const // * a scalar
{ Vector3D out( x*pR, y*pR, z*pR ); return out; }
friend Vector3D operator*(const real pR, const Vector3D pV)
{ Vector3D out( pV.x*pR, pV.y*pR, pV.z*pR ); return out; }
Vector3D& operator*=(const real pR)
{ x*=pR; y*=pR; z*=pR; return *this; }
Vector3D& operator=(const real pR)
{ x=pR; y=pR; z=pR; return *this; }
// magnitude
friend real len2(const Vector3D pV)
{ return pV.x*pV.x+pV.y*pV.y+pV.z*pV.z; }
friend real len(const Vector3D pV)
{ return sqrt(len2(pV)); }
// comparison
int operator==( const Vector3D pV) const
{ return x==pV.x && y==pV.y && z==pV.z; }
int operator!=( const Vector3D pV) const
{ return x!=pV.x || y!=pV.y || z!=pV.z; }
int operator<=( const Vector3D pV) const
{ return x<=pV.x && y<=pV.y && z<=pV.z; }
int operator>=( const Vector3D pV) const
{ return x>=pV.x && y>=pV.y && z>=pV.z; }
int operator<( const Vector3D pV) const
{ return x<pV.x && y<pV.y && z<pV.z; }
int operator>( const Vector3D pV) const
{ return x>pV.x && y>pV.y && z>pV.z; }
// input/output
//friend ostream& operator<<(ostream& pStr, const Vector3D& pV)
//{ return (pStr << '(' << pV.x << ',' << pV.y << ',' << pV.z << ')'); }
//friend istream& operator>>(istream pStr, Vector3D& pV)
//{ return (pStr >> pV.x >> pV.y >> pV.z); }
};