Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
matrix2i_ptr.c
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025-2026 Igal Alkon
2// SPDX-FileCopyrightText: 2026 ALKONTEK <git@alkontek.com>
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <vecmat.h>
6
13{
14 *res = (matrix2i){0};
15 res->v[0] = 1;
16 res->v[3] = 1;
17}
18
27void mat2i_mul_ptr(matrix2i *res, const matrix2i *a, const matrix2i *b)
28{
29 const matrix2i tmp = {
30 .m11 = a->m11 * b->m11 + a->m12 * b->m21,
31 .m21 = a->m21 * b->m11 + a->m22 * b->m21,
32 .m12 = a->m11 * b->m12 + a->m12 * b->m22,
33 .m22 = a->m21 * b->m12 + a->m22 * b->m22
34 };
35 *res = tmp;
36}
37
45{
46 *res = (matrix2i){
47 .m11 = m->v[0], .m21 = m->v[2],
48 .m12 = m->v[1], .m22 = m->v[3]
49 };
50}
51
61{
62 const vm_int_t det = mat2i_determinant(*m);
63 if (det == 0) {
65 return;
66 }
67 const double inv_det = 1.0 / (double)det;
68 *res = (matrix2i){
69 .m11 = (int)(m->v[3] * inv_det), .m21 = (int)(-m->v[1] * inv_det),
70 .m12 = (int)(-m->v[2] * inv_det), .m22 = (int)(m->v[0] * inv_det)
71 };
72}
73
81void mat2i_mul_vec2i_ptr(vector2i *res, const matrix2i *m, const vector2i *v)
82{
83 const vm_int_t x = v->x;
84 const vm_int_t y = v->y;
85 res->x = m->m11 * x + m->m12 * y;
86 res->y = m->m21 * x + m->m22 * y;
87}
void mat2i_transpose_ptr(matrix2i *res, const matrix2i *m)
Computes the transpose of the input integer 2x2 matrix and stores in res.
void mat2i_mul_ptr(matrix2i *res, const matrix2i *a, const matrix2i *b)
Multiplies two integer 2x2 matrices (a * b) using explicit loops and stores the result in res.
void mat2i_mul_vec2i_ptr(vector2i *res, const matrix2i *m, const vector2i *v)
Multiplies a 2x2 integer matrix by a vector2i.
void mat2i_inverse_ptr(matrix2i *res, const matrix2i *m)
Computes the inverse of the input integer 2x2 matrix and stores in res.
void mat2i_identity_ptr(matrix2i *res)
Initializes the integer 2x2 matrix to identity (diagonal 1, others 0).
vm_int_t m11
Definition vecmat.h:289
vm_int_t v[VECMAT_MAT2_SIZE]
Definition vecmat.h:294
vm_int_t m22
Definition vecmat.h:292
vm_int_t m21
Definition vecmat.h:290
vm_int_t m12
Definition vecmat.h:291
vm_int_t y
Definition vecmat.h:165
vm_int_t x
Definition vecmat.h:164
vm_int_t mat2i_determinant(matrix2i m)
Calculates the determinant of the given 2x2 integer matrix.
Definition matrix2i.c:73
int8_t vm_int_t
Definition vecmat.h:103