aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/geo/Vector2D.hpp
blob: e1d0f417ac9b36ae444259c96ac58bc2e5d2beb2 (plain) (blame)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "Vector2D.h"

#include "../math/utility.h"
#include "../math/Matrix.h"

#include <cmath>

namespace meow{
  template<class Scalar>
  inline
  Vector2D<Scalar>::Vector2D():
  _x(0),
  _y(0){
  }
  
  
  template<class Scalar>
  inline
  Vector2D<Scalar>::Vector2D(Vector2D const& __v):
  _x(__v._x),
  _y(__v._y){
  }
  
  
  template<class Scalar>
  inline
  Vector2D<Scalar>::Vector2D(Scalar const& __x, Scalar const& __y):
  _x(__x),
  _y(__y){
  }
  
  
  template<class Scalar>
  inline
  Vector2D<Scalar>::~Vector2D(){
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::copy(Vector2D const& __v){
    _x = __v.x();
    _y = __v.y();
    return *this;
  }
  
  template<class Scalar>
  inline Scalar const&
  Vector2D<Scalar>::x() const{
    return _x;
  }
  
  
  template<class Scalar>
  inline Scalar const&
  Vector2D<Scalar>::y() const{
    return _y;
  }
  
  
  template<class Scalar>
  inline Scalar const&
  Vector2D<Scalar>::x(Scalar const& __s){
    _x = __s;
    return x();
  }
  
  
  template<class Scalar>
  inline Scalar const&
  Vector2D<Scalar>::y(Scalar const& __s){
    _y = __s;
    return y();
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::xy(Scalar const& __x, Scalar const& __y){
    _x = __x;
    _y = __y;
    return *this;
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::positive() const{
    return *this;
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::negative() const{
    return Vector2D<Scalar>(-x(), -y());
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::right() const{
    return Vector2D<Scalar>(-y(), x());
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::add(Vector2D const& __v) const{
    return Vector2D<Scalar>(x() + __v.x(), y() + __v.y());
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::sub(Vector2D const& __v) const{
    return Vector2D<Scalar>(x() - __v.x(), y() - __v.y());
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::mul(Scalar const& __s) const{
    return Vector2D<Scalar>(x() * __s, y() * __s);
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::div(Scalar const& __s) const{
    return Vector2D<Scalar>(x() / __s, y() / __s);
  }
  
  
  template<class Scalar>
  inline Scalar
  Vector2D<Scalar>::dot(Vector2D const& __v) const{
    return (x() * __v.x() + y() * __v.y());
  }
  
  
  template<class Scalar>
  inline Scalar
  Vector2D<Scalar>::cross(Vector2D const& __v) const{
    return (x() * __v.y() - y() * __v.x());
  }
  
  
  template<class Scalar>
  inline Scalar
  Vector2D<Scalar>::length() const{
    return Scalar(sqrt(double(length2())));
  }
  
  
  template<class Scalar>
  inline Scalar
  Vector2D<Scalar>::length2() const{
    return squ(x()) + squ(y());
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::normalize(){
    return copy(normalized());
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::normalized() const{
    return div(length());
  }
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::rotate(Scalar const& __theta){
    return copy(rotated(__theta));
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::rotated(Scalar const& __theta) const{
    Scalar cs(cos(-double(__theta)));
    Scalar sn(sin(-double(__theta)));
    Vector2D<Scalar> new_x(cs, sn);
    return Vector2D<Scalar>(new_x.dot(*this), new_x.cross(*this));
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::reflect(Vector2D const& __v){
    return copy(reflected(__v));
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::reflected(Vector2D const& __v) const{
    return __v.mul(__v.dot(*this) * 2 / __v.length2()).sub(*this);
  }
  
  
  template<class Scalar>
  inline Matrix<Scalar>
  Vector2D<Scalar>::matrix() const{
    Matrix<Scalar> ret(2, 0, Scalar(0));
    ret(0, 0) = x();
    ret(1, 0) = y();
    return ret;
  }
  
  
  template<class Scalar>
  inline Matrix<Scalar>
  Vector2D<Scalar>::matrix(Scalar const& __homo) const{
    Matrix<Scalar> ret(3, 0, Scalar(0));
    ret(0, 0) = x();
    ret(1, 0) = y();
    ret(2, 0) = __homo;
    return ret;
  }
  
  
  template<class Scalar>
  inline Scalar const&
  Vector2D<Scalar>::operator()(size_t n) const{
    return (n == 0 ? x() : y());
  }
  
  
  template<class Scalar>
  inline Scalar&
  Vector2D<Scalar>::operator()(size_t n){
    return (n == 0 ? _x : _y);
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::operator()(Scalar const& __x, Scalar const& __y){
    x(__x);
    y(__y);
    return *this;
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator+() const{
    return positive();
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator-() const{
    return negative();
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator~() const{
    return right();
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator+(Vector2D const& __v) const{
    return add(__v);
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator-(Vector2D const& __v) const{
    return sub(__v);
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator*(Scalar const& __s) const{
    return mul(__s);
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>
  Vector2D<Scalar>::operator/(Scalar const& __s) const{
    return div(__s);
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::operator+=(Vector2D const& __v){
    return copy(add(__v));
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::operator-=(Vector2D const& __v){
    return copy(sub(__v));
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::operator*=(Scalar const& __s){
    return copy(mul(__s));
  }
  
  
  template<class Scalar>
  inline Vector2D<Scalar>&
  Vector2D<Scalar>::operator/=(Scalar const& __s){
    return copy(div(__s));
  }
}