Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
matrix2_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 = (matrix2){0};
15 res->v[0] = 1.0f;
16 res->v[3] = 1.0f;
17}
18
26void mat2_mul_ptr(matrix2 *res, const matrix2 *a, const matrix2 *b)
27{
28 const matrix2 tmp = {
29 .m11 = a->m11 * b->m11 + a->m12 * b->m21,
30 .m21 = a->m21 * b->m11 + a->m22 * b->m21,
31 .m12 = a->m11 * b->m12 + a->m12 * b->m22,
32 .m22 = a->m21 * b->m12 + a->m22 * b->m22
33 };
34 *res = tmp;
35}
36
44{
45 *res = (matrix2){
46 .m11 = m->v[0], .m21 = m->v[2],
47 .m12 = m->v[1], .m22 = m->v[3]
48 };
49}
50
51
60void mat2_inverse_ptr(matrix2 *res, const matrix2 *m)
61{
62 const vm_float_t det = mat2_determinant(*m);
63 if (det == 0.0f) {
65 return;
66 }
67 const vm_float_t inv_det = 1.0f / det;
68 *res = (matrix2){
69 .m11 = m->v[3] * inv_det, .m21 = -m->v[1] * inv_det,
70 .m12 = -m->v[2] * inv_det, .m22 = m->v[0] * inv_det
71 };
72}
73
81void mat2_mul_vec2_ptr(vector2 *res, const matrix2 *m, const vector2 *v)
82{
83 const vm_float_t x = v->x;
84 const vm_float_t y = v->y;
85 res->x = m->m11 * x + m->m12 * y;
86 res->y = m->m21 * x + m->m22 * y;
87}
88
97void mat2_rotation_z_ptr(matrix2 *res, const vm_float_t degrees)
98{
99 const vm_float_t radians = deg_to_rad(degrees);
100 const vm_float_t cos_theta = VECMAT_COS(radians);
101 const vm_float_t sin_theta = VECMAT_SIN(radians);
102 *res = (matrix2){
103 .m11 = cos_theta, .m21 = sin_theta,
104 .m12 = -sin_theta, .m22 = cos_theta
105 };
106}
107
114void mat2_scale_ptr(matrix2 *res, const vector2 *s)
115{
116 *res = (matrix2){
117 .m11 = s->x, .m21 = 0.0f,
118 .m12 = 0.0f, .m22 = s->y
119 };
120}
121
129{
130 *res = (matrix2){
131 .m11 = m->m11, .m21 = m->m21,
132 .m12 = m->m12, .m22 = m->m22
133 };
134}
void mat2_scale_ptr(matrix2 *res, const vector2 *s)
Builds a 2x2 scaling matrix from a vector2.
void mat2_mul_ptr(matrix2 *res, const matrix2 *a, const matrix2 *b)
Multiplies two 2x2 matrices (a * b) in column-major / column-vector convention.
Definition matrix2_ptr.c:26
void mat2_transpose_ptr(matrix2 *res, const matrix2 *m)
Computes the transpose of the input 2x2 matrix and stores in res.
Definition matrix2_ptr.c:43
void mat2_identity_ptr(matrix2 *res)
Initializes the 2x2 matrix to identity (diagonal 1.0, others 0.0).
Definition matrix2_ptr.c:12
void mat2_inverse_ptr(matrix2 *res, const matrix2 *m)
Computes the inverse of the input 2x2 matrix using determinant and stores in res.
Definition matrix2_ptr.c:60
void mat2_rotation_z_ptr(matrix2 *res, const vm_float_t degrees)
Sets res to a 2x2 rotation matrix for rotation around the Z-axis.
Definition matrix2_ptr.c:97
void mat2_mul_vec2_ptr(vector2 *res, const matrix2 *m, const vector2 *v)
Multiplies a 2x2 matrix by a vector2.
Definition matrix2_ptr.c:81
void mat2_from_mat3_ptr(matrix2 *res, const matrix3 *m)
Copies the upper-left 2x2 of a matrix3.
vm_float_t v[VECMAT_MAT2_SIZE]
Definition vecmat.h:213
vm_float_t m12
Definition vecmat.h:210
vm_float_t m11
Definition vecmat.h:208
vm_float_t m21
Definition vecmat.h:209
vm_float_t m22
Definition vecmat.h:211
vm_float_t m21
Definition vecmat.h:229
vm_float_t m12
Definition vecmat.h:231
vm_float_t m11
Definition vecmat.h:228
vm_float_t m22
Definition vecmat.h:232
vm_float_t y
Definition vecmat.h:128
vm_float_t x
Definition vecmat.h:127
#define VECMAT_SIN(x)
Definition vecmat.h:415
double vm_float_t
Definition vecmat.h:79
vm_float_t deg_to_rad(vm_float_t degrees)
Converts degrees to radians.
Definition vecmat.c:14
#define VECMAT_COS(x)
Definition vecmat.h:416
vm_float_t mat2_determinant(matrix2 m)
Calculates the determinant of the given 2x2 matrix.
Definition matrix2.c:88