]> git.bts.cx Git - benzene.git/blob - src/bz/math/matrix.c
Fixed Aseprite rendering system
[benzene.git] / src / bz / math / matrix.c
1 #include <bz/math/matrix.h>
2
3 #include <bz/math/math.h>
4
5 void bzMatrixSet(BZMatrix *matrixOut, float a, float b, float c, float d, float x, float y) {
6         matrixOut->a = a;
7         matrixOut->b = b;
8         matrixOut->c = c;
9         matrixOut->d = d;
10         matrixOut->x = x;
11         matrixOut->y = y;
12 }
13
14 void bzMatrixCopy(BZMatrix *matrixOut, const BZMatrix *matrix) {
15         float a = matrix->a;
16         float b = matrix->b;
17         float c = matrix->c;
18         float d = matrix->d;
19         float x = matrix->x;
20         float y = matrix->y;
21         bzMatrixSet(matrixOut, a, b, c, d, x, y);
22 }
23
24 void bzMatrixInverse(BZMatrix *matrixOut, const BZMatrix *matrix) {
25         float det = 1.0f / (matrix->a * matrix->d - matrix->b * matrix->c);
26         float a = matrix->d * det;
27         float b = -(matrix->b) * det;
28         float c = -(matrix->c) * det;
29         float d = matrix->a * det;
30         float x = (matrix->b * matrix->y - matrix->d * matrix->x) * det;
31         float y = -(matrix->a * matrix->y - matrix->c * matrix->x) * det;
32         bzMatrixSet(matrixOut, a, b, c, d, x, y);
33 }
34
35 void bzMatrixMultiply(BZMatrix *matrixOut, const BZMatrix *matrix1, const BZMatrix *matrix2) {
36         float a = matrix1->a * matrix2->a + matrix1->b * matrix2->c;
37         float b = matrix1->a * matrix2->b + matrix1->b * matrix2->d;
38         float c = matrix1->c * matrix2->a + matrix1->d * matrix2->c;
39         float d = matrix1->c * matrix2->b + matrix1->d * matrix2->d;
40         float x = matrix1->a * matrix2->x + matrix1->b * matrix2->y + matrix1->x;
41         float y = matrix1->c * matrix2->x + matrix1->d * matrix2->y + matrix1->y;
42         bzMatrixSet(matrixOut, a, b, c, d, x, y);
43 }
44
45 void bzMatrixIdentity(BZMatrix *matrixOut) {
46         bzMatrixSet(matrixOut, 1, 0, 0, 1, 0, 0);
47 }
48
49 void bzMatrixRotation(BZMatrix *matrixOut, float angle) {
50         float ct = bzCosine(angle);
51         float st = bzSine(angle);
52         bzMatrixSet(matrixOut, ct, -st, st, ct, 0, 0);
53 }
54
55 void bzMatrixTranslation(BZMatrix *matrixOut, float translateX, float translateY) {
56         bzMatrixSet(matrixOut, 1, 0, 0, 1, translateX, translateY);
57 }
58
59 void bzMatrixScale(BZMatrix *matrixOut, float scaleX, float scaleY) {
60         bzMatrixSet(matrixOut, scaleX, 0, 0, scaleY, 0, 0);
61 }
62
63 /*void bzMatrixTRS(BZMatrix *matrixOut, float translateX, float translateY, float angle, float scaleX, float scaleY) {
64         BZMatrix t, r, s;
65         bzMatrixTranslation(&t, translateX, translateY);
66         bzMatrixRotation(&r, angle);
67         bzMatrixScale(&s, scaleX, scaleY);
68         bzMatrixMultiply(&r, &r, &s);
69         bzMatrixMultiply(&t, &t, &r);
70         bzMatrixCopy(matrixOut, &t);
71         //float ct = bzCosine(angle);
72         //float st = bzSine(angle);
73         //bzMatrixSet(matrixOut, scaleX * ct, scaleX * -st, scaleY * st, scaleY * ct, translateX, translateY);
74 }*/
75
76 void bzMatrixSRT(BZMatrix *matrixOut, float translateX, float translateY, float angle, float scaleX, float scaleY) {
77         float ct = bzCosine(angle);
78         float st = bzSine(angle);
79         bzMatrixSet(matrixOut, scaleX * ct, scaleX * -st, scaleY * st, scaleY * ct, translateX, translateY);
80
81 /*      BZMatrix s, r, t;
82         bzMatrixScale(&s, scaleX, scaleY);
83         bzMatrixRotation(&r, angle);
84         bzMatrixTranslation(&t, translateX, translateY);
85         bzMatrixMultiply(&r, &r, &s);
86         bzMatrixMultiply(&t, &t, &r);
87         bzMatrixCopy(matrixOut, &t);*/
88 //      bzMatrixMultiply(&r, &r, &t);
89 //      bzMatrixMultiply(&s, &s, &r);
90 //      bzMatrixCopy(matrixOut, &s);
91 //      float ct = bzCosine(angle);
92 //      float st = bzSine(angle);
93 //      bzMatrixSet(matrixOut, scaleX * ct, scaleX * -st, scaleY * st, scaleY * ct, translateX, translateY);
94 }
95
96 void bzMatrixShear(BZMatrix *matrixOut, float shearX, float shearY) {
97         bzMatrixSet(matrixOut, 1, shearX, shearY, 1, 0, 0);
98 }
99
100 void bzMatrixOrtho(BZMatrix *matrixOut, float left, float top, float right, float bottom) {
101         float a = 2.0f / (right - left);
102         float d = 2.0f / (top - bottom);
103         float x = -(right + left) / (right - left);
104         float y = -(top + bottom) / (top - bottom);
105         bzMatrixSet(matrixOut, a, 0, 0, d, x, y);
106 }
107
108 void bzMatrixScaleVector(BZVector *vectorOut, const BZVector *vector, const BZMatrix *matrix) {
109         float x = vector->x * bzSqrt(matrix->a * matrix->a + matrix->b * matrix->b);
110         float y = vector->y * bzSqrt(matrix->c * matrix->c + matrix->d * matrix->d);
111         bzVectorSet(vectorOut, x, y);
112 }
113
114 void bzMatrixScaleRotateVector(BZVector *vectorOut, const BZVector *vector, const BZMatrix *matrix) {
115         float x = (vector->x * matrix->a) + (vector->y * matrix->b);
116         float y = (vector->x * matrix->c) + (vector->y * matrix->d);
117         bzVectorSet(vectorOut, x, y);
118 }
119
120 void bzMatrixTranslateVector(BZVector *vectorOut, const BZVector *vector, const BZMatrix *matrix) {
121         float x = vector->x + matrix->x;
122         float y = vector->y + matrix->y;
123         bzVectorSet(vectorOut, x, y);
124 }
125
126 void bzMatrixTransformVector(BZVector *vectorOut, const BZVector *vector, const BZMatrix *matrix) {
127         float x = (vector->x * matrix->a) + (vector->y * matrix->b) + matrix->x;
128         float y = (vector->x * matrix->c) + (vector->y * matrix->d) + matrix->y;
129         bzVectorSet(vectorOut, x, y);
130 }