-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.h
64 lines (49 loc) · 1.71 KB
/
matrix.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
#ifndef MATRIX_H
#define MATRIX_H
#include "mytypes.h"
#if 1 // C placeholder
typedef struct {} Matrix;
inline Vec2 matrix_xform(Matrix *m, Vec2 v);
#else // C++ ... porting TODO
#include <string>
#include <cstdio>
#include <Box2D/Box2D.h>
class Matrix {
public:
float32 val[6];
// Constructors
Matrix(std::string m);
Matrix(float32 v1, float32 v2, float32 v3, float32 v4, float32 v5, float32 v6) {
val[0] = v1; val[1] = v2; val[2] = v3;
val[3] = v4; val[4] = v5; val[5] = v6;
}
// Inverse
Matrix *inverse();
b2Vec2 xform(b2Vec2 v);
b2Vec2 call(b2Vec2 pt);
void dump() {
printf("%f,%f,%f,%f,%f,%f\n", val[0],val[1],val[2],val[3],val[4],val[5]);
}
};
inline b2Vec2 Matrix::call(b2Vec2 pt) {
return b2Vec2(val[0]*pt.x + val[2]*pt.y + val[4],
val[1]*pt.x + val[3]*pt.y + val[5]);
}
inline Matrix *Matrix::inverse() {
float32 d = val[0]*val[3] - val[1]*val[2];
return new Matrix(val[3]/d, -val[1]/d, -val[2]/d, val[0]/d,
(val[2]*val[5] - val[3]*val[4])/d,
(val[1]*val[4] - val[0]*val[5])/d);
}
inline b2Vec2 Matrix::xform(b2Vec2 v) {
return b2Vec2(val[0]*v.x + val[2]*v.y + val[4], val[1]*v.x + val[3]*v.y + val[5]);
}
inline Matrix * mul(Matrix *m1, Matrix *m2) {
float32 a = m1->val[0]; float32 b = m1->val[1]; float32 c = m1->val[2];
float32 d = m1->val[3]; float32 e = m1->val[4]; float32 f = m1->val[5];
float32 u = m2->val[0]; float32 v = m2->val[1]; float32 w = m2->val[2];
float32 x = m2->val[3]; float32 y = m2->val[4]; float32 z = m2->val[5];
return new Matrix(a*u + c*v, b*u + d*v, a*w + c*x, b*w + d*x, a*y + c*z + e, b*y + d*z + f);
}
#endif
#endif