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
|
#include "mons_math/mat2.h"
const mons_mat2 MONS_MAT2_ZERO = {
{0, 0},
{0, 0},
};
const mons_mat2 MONS_MAT2_IDENTITY = {
{1, 0},
{0, 1},
};
mons_mat2 mons_mat2_add(mons_mat2 a, mons_mat2 b) {
mons_mat2 result = {
mons_vec2_add(a.m1, b.m1),
mons_vec2_add(a.m2, b.m2),
};
return result;
}
void mons_mat2_add_inplace(mons_mat2 *a, mons_mat2 b) {
mons_vec2_add_inplace(&a->m1, b.m1);
mons_vec2_add_inplace(&a->m2, b.m2);
}
mons_mat2 mons_mat2_mul_f(mons_mat2 a, float b) {
mons_mat2 result = {
mons_vec2_mul_f(a.m1, b),
mons_vec2_mul_f(a.m2, b),
};
return result;
}
mons_mat2 mons_mat2_mul_i(mons_mat2 a, int b) {
mons_mat2 result = {
mons_vec2_mul_i(a.m1, b),
mons_vec2_mul_i(a.m2, b),
};
return result;
}
mons_mat2 mons_mat2_mul(mons_mat2 a, mons_mat2 b) {
mons_vec2 b_n1 = mons_mat2_n1(b);
mons_vec2 b_n2 = mons_mat2_n2(b);
mons_mat2 result = {
{
mons_vec2_dot(a.m1, b_n1),
mons_vec2_dot(a.m1, b_n2),
},
{
mons_vec2_dot(a.m2, b_n1),
mons_vec2_dot(a.m2, b_n2),
},
};
return result;
}
void mons_mat2_mul_f_inplace(mons_mat2 *a, float b) {
mons_vec2_mul_f_inplace(&a->m1, b);
mons_vec2_mul_f_inplace(&a->m2, b);
}
void mons_mat2_mul_i_inplace(mons_mat2 *a, int b) {
mons_vec2_mul_i_inplace(&a->m1, b);
mons_vec2_mul_i_inplace(&a->m2, b);
}
mons_mat2 mons_mat2_transpose(mons_mat2 a) {
mons_mat2 result = {
mons_mat2_n1(a),
mons_mat2_n2(a),
};
return result;
}
void mons_mat2_transpose_inplace(mons_mat2 *a) { *a = mons_mat2_transpose(*a); }
mons_vec2 mons_mat2_n1(mons_mat2 a) {
mons_vec2 result = {
a.m1.x,
a.m2.x,
};
return result;
}
mons_vec2 mons_mat2_n2(mons_mat2 a) {
mons_vec2 result = {
a.m1.y,
a.m2.y,
};
return result;
}
float mons_mat2_determinant(mons_mat2 a) {
// ad - bc
return (a.m1.x * a.m2.y) - (a.m1.y * a.m2.x);
}
mons_mat2 mons_mat2_adjoint(mons_mat2 a) {
/*
* d -b
* -c a
*/
mons_mat2 result = {{
a.m2.y,
-a.m1.y,
},
{
-a.m2.x,
a.m1.x,
}};
return result;
}
mons_mat2 mons_mat2_inverse(mons_mat2 a) {
return mons_mat2_mul_f(mons_mat2_adjoint(a),
1.0 / mons_mat2_determinant(a));
}
int mons_mat2_equal(mons_mat2 a, mons_mat2 b) {
return mons_vec2_equal(a.m1, b.m1) && mons_vec2_equal(a.m2, b.m2);
}
|