/
vec.h
164 lines (125 loc) · 3.73 KB
/
vec.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/////////////////////////////////////////
//
// OpenLieroX
//
// Auxiliary Software class library
//
// based on the work of JasonB
// enhanced by Dark Charlie and Albert Zeyer
//
// code under LGPL
//
/////////////////////////////////////////
// 2D Vector / Matrix class
// Created 20/11/01
// By Jason Boettcher
// enhanced by Albert Zeyer
#ifndef __CVEC_H__
#define __CVEC_H__
#include <cmath>
template<typename _T>
struct VectorD2 {
// Constructor
VectorD2() : x(0), y(0) {}
VectorD2(_T _x, _T _y) : x(_x), y(_y) {}
// Attributes
_T x, y;
// Methods
template<typename _T2>
VectorD2(const _T2& cp) {
x = (_T)cp.x; y = (_T)cp.y;
}
float GetLength() const { return sqrtf((float)x*x + (float)y*y); }
_T GetLength2() const { return x*x + y*y; };
float GetAngle() const { return (float)atan2((float)y,(float)x); }
VectorD2 Normalize() { return *this/GetLength(); }
_T Scalar(const VectorD2& vec) const { return x*vec.x + y*vec.y; }
VectorD2 orthogonal() const { return VectorD2(y, -x); }
_T Cross(const VectorD2& oth) const { return x * oth.y - y * oth.x; }
VectorD2 operator*(_T scalar) const {
return VectorD2(x*scalar,y*scalar);
}
VectorD2 operator/(_T scalar) const {
return VectorD2(x/scalar,y/scalar);
}
VectorD2 operator+(const VectorD2& vec) const {
return VectorD2(x+vec.x,y+vec.y);
}
VectorD2 operator-(const VectorD2& vec) const {
return VectorD2(x-vec.x,y-vec.y);
}
VectorD2 operator-() const {
return VectorD2(-x,-y);
}
VectorD2& operator+=(const VectorD2& vec) {
x+=vec.x; y+=vec.y;
return *this;
}
VectorD2& operator-=(const VectorD2& vec) {
x-=vec.x; y-=vec.y;
return *this;
}
VectorD2& operator*=(_T scalar) {
x*=scalar; y*=scalar;
return *this;
}
template<typename _T2>
bool operator<(const VectorD2<_T2> & op) const {
return ((y == op.y && (x < op.x))
|| y < op.y);
}
template<typename _T2>
bool operator==(const VectorD2<_T2> & op) const {
return (x==op.x && y==op.y);
}
template<typename _T2>
bool operator!=(const VectorD2<_T2> & op) const {
return (x!=op.x || y!=op.y);
}
template<typename _T2>
bool operator<=(const VectorD2<_T2> & op) const {
return ((*this < op) || (*this == op));
}
};
template<typename _T>
struct VectorD2__absolute_less {
VectorD2<_T> zero;
VectorD2__absolute_less(VectorD2<_T> z = VectorD2<_T>(0,0)) : zero(z) {}
bool operator()(const VectorD2<_T> v1, const VectorD2<_T> v2) const {
return (v1-zero).GetLength2() < (v2-zero).GetLength2();
}
};
typedef VectorD2<float> VecF;
typedef VectorD2<int> Vec;
typedef Vec Point;
typedef VectorD2<double> VecD;
template<typename _T>
struct MatrixD2 {
VectorD2<_T> v1;
VectorD2<_T> v2;
MatrixD2() {}
MatrixD2(_T f) { v1.x = f; v2.y = f; }
MatrixD2(_T x1, _T y1, _T x2, _T y2) : v1(x1,y1), v2(x2,y2) {}
MatrixD2(const VectorD2<_T>& _v1, const VectorD2<_T>& _v2) : v1(_v1), v2(_v2) {}
static MatrixD2 Rotation(_T x, _T y) { return MatrixD2(x,y,-y,x); }
template<typename _T2> bool operator==(const MatrixD2<_T2>& m) const { return v1 == m.v1 && v2 == m.v2; }
template<typename _T2> bool operator!=(const MatrixD2<_T2>& m) const { return !(*this == m); }
MatrixD2<_T> operator+(const MatrixD2<_T>& m) const {
return MatrixD2<_T>(v1 + m.v1, v2 + m.v2);
}
MatrixD2<_T> operator-(const MatrixD2<_T>& m) const {
return MatrixD2<_T>(v1 - m.v1, v2 - m.v2);
}
VectorD2<_T> operator*(const VectorD2<_T>& v) const {
return VectorD2<_T>( v1.x * v.x + v2.x * v.y, v1.y * v.x + v2.y * v.y );
}
MatrixD2<_T> operator*(const MatrixD2<_T>& m) const {
return MatrixD2<_T>( *this * m.v1, *this * m.v2 );
}
MatrixD2<_T>& operator*=(const MatrixD2<_T>& m) {
return *this = *this * m;
}
};
typedef MatrixD2<int> Matrix;
typedef MatrixD2<double> MatD;
#endif // __CVEC_H__