KisaragiLibrary
 
読み取り中…
検索中…
一致する文字列を見つけられません
Matrix2D.hpp
[詳解]
1#pragma once
2
3#include <array>
4#include "Const.h"
6#include <cmath>
8
9
10using std::array;
12
13namespace Kisaragi_Lib
14{
15
21
22 //matrix2x2は平行移動ができない
24 {
25 private:
26 array<array<double, 3>, 3>matrix;
27 /*
28 | [_][0] |[_][1] |[_][2]
29 [0][_]|sx⋅cos(θ) | −sy⋅sin(θ) | tx
30 [1][_]|sx⋅sin(θ) | sy⋅cos(θ) | ty
31 [2][_]| 0 | 0 | 1
32
33 sxとsyはそれぞれx方向とy方向のスケール係数、
34 θは回転角度(ラジアン)、
35 txとtyは平行移動の成分です。
36
37 */
38
39 public:
41 {
42 ZeroClear();
43 }
44
46 {
47 switch (_type)
48 {
50 ZeroClear();
51 break;
52
54 ObjectInit();
55 break;
56 default:
57 break;
58 }
59
60 }
61
62 void ZeroClear()
63 {
64 //0で初期化する
65 for (int i = 0; i < 3; i++)
66 {
67 for (int j = 0; j < 3; j++)
68 {
69 matrix[i][j] = 0;
70 }
71 }
72 }
73
75 {
76 ZeroClear();
77
78 //サイズとzを1にする
79 for (int i = 0; i < 3; i++)
80 {
81 matrix[i][i] = 1;
82 }
83 }
84
85#pragma region ゲッタ
86 //確認済み
88 {
89 return Point2D<double>(matrix[0][2], matrix[1][2]);
90 }
91
92 /// <summary>
93 /// 回転をラジアン値で返す
94 /// </summary>
95 /// <returns>ラジアン値を返す</returns>
96 double Rotation()
97 {
98 // atan2を使用して回転角度を取得
99 return atan2(matrix[1][0], matrix[0][0]);
100 }
101
102 //TODO debug
104 {
105 double tmpX, tmpY;
106
107 tmpX = std::sqrt(Math::Pow(matrix[0][0],2) + Math::Pow(matrix[1][0], 2));
108
109 tmpY = std::sqrt(Math::Pow(matrix[0][1], 2) + Math::Pow(matrix[1][1], 2));
110
111 return Point2D<double>(tmpX, tmpY);
112 }
113
114#pragma endregion
115
116 //確認済み(平行移動行列)
118 {
119 Matrix2D tmp(ZERO_CLEAR);
120
121 for (int i = 0; i < 3; i++)
122 {
123 for (int j = 0; j < 3; j++)
124 {
125 for (int k = 0; k < 3; k++)
126 {
127 tmp.matrix[i][j] += matrix[i][k] * _mat.matrix[k][j];
128 }
129 }
130 }
131
132 matrix = tmp.matrix;
133 }
134
135 //確認済み(平行移動行列)
137 {
138 Matrix2D tmp(ZERO_CLEAR);
139
140 for (int i = 0; i < 3; i++)
141 {
142 for (int j = 0; j < 3; j++)
143 {
144 for (int k = 0; k < 3; k++)
145 {
146 tmp.matrix[i][j] += matrix[i][k] * _mat.matrix[k][j];
147 }
148 }
149 }
150
151 return tmp;
152 }
153
154
155 Matrix2D operator*(double scalar)
156 {
157 Matrix2D result(*this); // 現在の行列をコピー
158
159 // 行列の各要素にスカラーを掛ける
160 for (int i = 0; i < 3; ++i)
161 {
162 for (int j = 0; j < 3; ++j)
163 {
164 result.matrix[i][j] *= scalar;
165 }
166 }
167
168 return result;
169 }
170
171 const Matrix2D& operator*=(double scalar)
172 {
173 // 行列の各要素にスカラーを掛ける
174 for (int i = 0; i < 3; ++i)
175 {
176 for (int j = 0; j < 3; ++j)
177 {
178 matrix[i][j] *= scalar;
179 }
180 }
181
182 return *this;
183 }
184
185#pragma region 移動行列作成関数
186 //確認済み
187 static Matrix2D CreateMoveMatrix(double _x,double _y)
188 {
190
191 tmp.matrix[0][2] = _x;
192 tmp.matrix[1][2] = _y;
193
194 return tmp;
195 }
196
198 {
200
201 tmp.matrix[0][2] = Position().x;
202 tmp.matrix[1][2] = Position().y;
203
204 return tmp;
205 }
206
207 //radianだよ
208 static Matrix2D CreateRotaMatrix(double _angle)
209 {
210 Matrix2D tmp(ZERO_CLEAR);
211
212 double cos = std::cos(_angle);
213 double sin = std::sin(_angle);
214
215 tmp.matrix[0][0] = cos;
216 tmp.matrix[1][1] = cos;
217
218 tmp.matrix[0][1] = -sin;
219 tmp.matrix[1][0] = sin;
220
221 tmp.matrix[2][2] = 1;
222
223 return tmp;
224 }
225
227 {
228 Matrix2D tmp(ZERO_CLEAR);
229
230 double cos = std::cos(Rotation());
231 double sin = std::sin(Rotation());
232
233 tmp.matrix[0][0] = cos;
234 tmp.matrix[1][1] = cos;
235
236 tmp.matrix[0][1] = -sin;
237 tmp.matrix[1][0] = sin;
238
239 tmp.matrix[2][2] = 1;
240
241 return tmp;
242 }
243
244 static Matrix2D CreateScaleMatrix(double _x,double _y)
245 {
246 //0以下にはなりません
247 assert(_x > 0);
248 assert(_y > 0);
249
250 Matrix2D tmp(ZERO_CLEAR);
251
252 tmp.matrix[0][0] = _x;
253 tmp.matrix[1][1] = _y;
254 tmp.matrix[2][2] = 1;
255
256 return tmp;
257 }
258
260 {
261 //0以下にはなりません
262 assert(_scale.x > 0);
263 assert(_scale.y > 0);
264
265 Matrix2D tmp(ZERO_CLEAR);
266
267 tmp.matrix[0][0] = _scale.x;
268 tmp.matrix[1][1] = _scale.y;
269 tmp.matrix[2][2] = 1;
270
271 return tmp;
272 }
273
275 {
276 //0以下にはなりません
277 assert(Scale().x > 0);
278 assert(Scale().y > 0);
279
280 Matrix2D tmp(ZERO_CLEAR);
281
282 tmp.matrix[0][0] = Scale().x;
283 tmp.matrix[1][1] = Scale().y;
284 tmp.matrix[2][2] = 1;
285
286 return tmp;
287 }
288#pragma endregion
289
290#pragma region 逆行列作成関数
292 {
294 tmp *= Matrix2D::CreateMoveMatrix(Position().x * -1, Position().y * -1);
295
296 return tmp;
297 }
298
300 {
302 tmp *= Matrix2D::CreateMoveMatrix(_pos.x * -1, _pos.y * -1);
303
304 return tmp;
305 }
306
308 {
311
312 return tmp;
313 }
314
315 template <class T>
317 {
319 tmp *= Matrix2D::CreateRotaMatrix(_rotation * -1);
320
321 return tmp;
322 }
323
325 {
327 tmp *= Matrix2D::CreateScaleMatrix(1 / Scale().x, 1 / Scale().y);
328
329 return tmp;
330 }
331
332 template <class T>
334 {
336 tmp *= Matrix2D::CreateScaleMatrix(1 / _scale.x, 1 / _scale.y);
337
338 return tmp;
339 }
340
341 //TODO Debug
343 {
345
346 // 移動の逆行列
348
349 // 回転の逆行列
351
352 // スケーリングの逆行列
354
355 return tmp;
356 }
357
358#pragma endregion
359
360 /// <summary>
361 /// X軸、Y軸に平行移動する
362 /// 回転の影響を受けない.
363 /// </summary>
364 /// <typeparam name="T"></typeparam>
365 /// <param name="_x"></param>
366 /// <param name="_y"></param>
367 template<class T>
368 void AxisAlignedTranslation(T _x, T _y)
369 {
370 matrix[0][2] += _x;
371 matrix[1][2] += _y;
372 }
373
374 /// <summary>
375 /// X軸、Y軸に平行な座標をセットする
376 /// 回転、拡大の影響を受けない
377 /// </summary>
378 /// <typeparam name="T"></typeparam>
379 /// <param name="_x"></param>
380 /// <param name="_y"></param>
381 template<class T>
383 {
384 matrix[0][2] = _x;
385 matrix[1][2] = _y;
386 }
387
388 };
389}
独自のMathを提供します
図形の形状を表すためのクラスを提供するヘッダ
汎用計算クラス
Definition KisaragiMath.hpp:39
static T Pow(const T &_num, const unsigned int &_cnt)
べき乗,累乗
Definition KisaragiMath.hpp:126
Matrix2D CreateInverseScaleMatrix()
Definition Matrix2D.hpp:324
Matrix2D operator*(Matrix2D _mat)
Definition Matrix2D.hpp:136
Matrix2D CreateInverseMoveMatrix()
Definition Matrix2D.hpp:291
static Matrix2D CreateInverseMoveMatrix(Point2D< double > _pos)
Definition Matrix2D.hpp:299
Point2D< double > Position()
Definition Matrix2D.hpp:87
void AxisAlignedTranslation(T _x, T _y)
X軸、Y軸に平行移動する 回転の影響を受けない.
Definition Matrix2D.hpp:368
static Matrix2D CreateMoveMatrix(double _x, double _y)
Definition Matrix2D.hpp:187
Matrix2D(Matrix2DType _type)
Definition Matrix2D.hpp:45
static Matrix2D CreateRotaMatrix(double _angle)
Definition Matrix2D.hpp:208
Matrix2D CreateRotaMatrix()
Definition Matrix2D.hpp:226
static Matrix2D CreateScaleMatrix(Scale2D< double > _scale)
Definition Matrix2D.hpp:259
void ObjectInit()
Definition Matrix2D.hpp:74
Matrix2D operator*(double scalar)
Definition Matrix2D.hpp:155
static Matrix2D CreateScaleMatrix(double _x, double _y)
Definition Matrix2D.hpp:244
array< array< double, 3 >, 3 > matrix
Definition Matrix2D.hpp:26
Matrix2D CreateInverseTransformationMatrix()
Definition Matrix2D.hpp:342
static Matrix2D CreateInverseScaleMatrix(Scale2D< T > _scale)
Definition Matrix2D.hpp:333
static Matrix2D CreateInverseRotaMatrix(T _rotation)
Definition Matrix2D.hpp:316
Matrix2D CreateScaleMatrix()
Definition Matrix2D.hpp:274
double Rotation()
回転をラジアン値で返す
Definition Matrix2D.hpp:96
Matrix2D()
Definition Matrix2D.hpp:40
Matrix2D CreateMoveMatrix()
Definition Matrix2D.hpp:197
const Matrix2D & operator*=(double scalar)
Definition Matrix2D.hpp:171
Matrix2D CreateInverseRotaMatrix()
Definition Matrix2D.hpp:307
void ZeroClear()
Definition Matrix2D.hpp:62
void SetAxisAlignedTranslation(T _x, T _y)
X軸、Y軸に平行な座標をセットする 回転、拡大の影響を受けない
Definition Matrix2D.hpp:382
Point2D< double > Scale()
Definition Matrix2D.hpp:103
void operator*=(Matrix2D _mat)
Definition Matrix2D.hpp:117
Definition Accessor.hpp:110
Matrix2DType
Definition Matrix2D.hpp:17
@ IDENTITY_MATRIX
Definition Matrix2D.hpp:19
@ ZERO_CLEAR
Definition Matrix2D.hpp:18
Point2D< T > Scale2D
サイズを表すプリミティブ型 実体はPoint2D<T>と変わりありません
Definition Primitive.hpp:193
点を表すプリミティブ型
Definition Primitive.hpp:29
T y
Definition Primitive.hpp:30
T x
Definition Primitive.hpp:30