Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vecmat.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 <stdlib.h>
6#include <vecmat.h>
7
15{
16 return (vm_float_t)((double)degrees * VM_DEG_TO_RAD);
17}
18
26{
27 return (vm_float_t)((double)radians * VM_RAD_TO_DEG);
28}
29
37bool vec2_eq(const vector2 a, const vector2 b)
38{
39 return VECMAT_FABS(a.x - b.x) < VECMAT_EPSILON
40 && VECMAT_FABS(a.y - b.y) < VECMAT_EPSILON;
41}
42
50bool vec3_eq(const vector3 a, const vector3 b)
51{
52 return VECMAT_FABS(a.x - b.x) < VECMAT_EPSILON
53 && VECMAT_FABS(a.y - b.y) < VECMAT_EPSILON
54 && VECMAT_FABS(a.z - b.z) < VECMAT_EPSILON;
55}
56
64bool vec4_eq(const vector4 a, const vector4 b)
65{
66 return VECMAT_FABS(a.x - b.x) < VECMAT_EPSILON
67 && VECMAT_FABS(a.y - b.y) < VECMAT_EPSILON
68 && VECMAT_FABS(a.z - b.z) < VECMAT_EPSILON
69 && VECMAT_FABS(a.w - b.w) < VECMAT_EPSILON;
70}
71
79bool vec2i_eq(const vector2i a, const vector2i b)
80{
81 return a.x == b.x && a.y == b.y;
82}
83
91bool vec3i_eq(const vector3i a, const vector3i b)
92{
93 return a.x == b.x && a.y == b.y && a.z == b.z;
94}
95
103bool vec4i_eq(const vector4i a, const vector4i b)
104{
105 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
106}
107
115bool mat2_eq(const matrix2 a, const matrix2 b)
116{
117 for (int i = 0; i < 4; i++) {
118 if (VECMAT_FABS(a.v[i] - b.v[i]) >= VECMAT_EPSILON) return false;
119 }
120 return true;
121}
122
130bool mat3_eq(const matrix3 a, const matrix3 b)
131{
132 for (int i = 0; i < 9; i++) {
133 if (VECMAT_FABS(a.v[i] - b.v[i]) >= VECMAT_EPSILON) return false;
134 }
135 return true;
136}
137
145bool mat4_eq(const matrix4 a, const matrix4 b)
146{
147 for (int i = 0; i < 16; i++) {
148 if (VECMAT_FABS(a.v[i] - b.v[i]) >= VECMAT_EPSILON) return false;
149 }
150 return true;
151}
152
160bool mat2i_eq(const matrix2i a, const matrix2i b)
161{
162 for (int i = 0; i < 4; i++) {
163 if (a.v[i] != b.v[i]) return false;
164 }
165 return true;
166}
167
175bool mat3i_eq(const matrix3i a, const matrix3i b)
176{
177 for (int i = 0; i < 9; i++) {
178 if (a.v[i] != b.v[i]) return false;
179 }
180 return true;
181}
182
190bool mat4i_eq(const matrix4i a, const matrix4i b)
191{
192 for (int i = 0; i < 16; i++) {
193 if (a.v[i] != b.v[i]) return false;
194 }
195 return true;
196}
197
205bool quat_eq(const quaternion a, const quaternion b)
206{
207 return VECMAT_FABS(a.x - b.x) < VECMAT_EPSILON
208 && VECMAT_FABS(a.y - b.y) < VECMAT_EPSILON
209 && VECMAT_FABS(a.z - b.z) < VECMAT_EPSILON
210 && VECMAT_FABS(a.w - b.w) < VECMAT_EPSILON;
211}
212
221{
222 return (vector2){ .x = x, .y = y };
223}
224
231void vec2_assign(vector2 *dest, const vector2 *src)
232{
233 dest->x = src->x;
234 dest->y = src->y;
235}
236
244void vec2_assign_xy(vector2 *dest, const vm_float_t x, const vm_float_t y)
245{
246 dest->x = x;
247 dest->y = y;
248}
249
256void vec2_add_assign(vector2 *dest, const vector2 *src)
257{
258 dest->x += src->x;
259 dest->y += src->y;
260}
261
269{
270 return (vector2){ .x = (vm_float_t)v->x, .y = (vm_float_t)v->y };
271}
272
281{
282 return (vector2i){ .x = x, .y = y };
283}
284
291void vec2i_assign(vector2i *dest, const vector2i *src)
292{
293 dest->x = src->x;
294 dest->y = src->y;
295}
296
304void vec2i_assign_xy(vector2i *dest, const vm_int_t x, const vm_int_t y)
305{
306 dest->x = x;
307 dest->y = y;
308}
309
316void vec2i_add_assign(vector2i *dest, const vector2i *src)
317{
318 dest->x += src->x;
319 dest->y += src->y;
320}
321
329{
330 return (vector2i){ .x = (vm_int_t)v->x, .y = (vm_int_t)v->y };
331}
332
340{
341 return (vector2i){ .x = (vm_int_t)VECMAT_FLOOR(v->x), .y = (vm_int_t)VECMAT_FLOOR(v->y) };
342}
343
351{
352 return (vector2i){ .x = (vm_int_t)VECMAT_ROUND(v->x), .y = (vm_int_t)VECMAT_ROUND(v->y) };
353}
354
363vector3 vec3(const vm_float_t x, const vm_float_t y, const vm_float_t z)
364{
365 return (vector3){ .x = x, .y = y, .z = z };
366}
367
374void vec3_assign(vector3 *dest, const vector3 *src)
375{
376 dest->x = src->x;
377 dest->y = src->y;
378 dest->z = src->z;
379}
380
389void vec3_assign_xyz(vector3 *dest, const vm_float_t x, const vm_float_t y, const vm_float_t z)
390{
391 dest->x = x;
392 dest->y = y;
393 dest->z = z;
394}
395
402void vec3_add_assign(vector3 *dest, const vector3 *src)
403{
404 dest->x += src->x;
405 dest->y += src->y;
406 dest->z += src->z;
407}
408
416{
417 return (vector3){
418 .x = (vm_float_t)v->x,
419 .y = (vm_float_t)v->y,
420 .z = (vm_float_t)v->z
421 };
422}
423
432vector3i vec3i(const vm_int_t x, const vm_int_t y, const vm_int_t z)
433{
434 return (vector3i){ .x = x, .y = y, .z = z };
435}
436
443void vec3i_assign(vector3i *dest, const vector3i *src)
444{
445 dest->x = src->x;
446 dest->y = src->y;
447 dest->z = src->z;
448}
449
458void vec3i_assign_xyz(vector3i *dest, const vm_int_t x, const vm_int_t y, const vm_int_t z)
459{
460 dest->x = x;
461 dest->y = y;
462 dest->z = z;
463}
464
471void vec3i_add_assign(vector3i *dest, const vector3i *src)
472{
473 dest->x += src->x;
474 dest->y += src->y;
475 dest->z += src->z;
476}
477
485{
486 return (vector3i){
487 .x = (vm_int_t)v->x,
488 .y = (vm_int_t)v->y,
489 .z = (vm_int_t)v->z
490 };
491}
492
500{
501 return (vector3i){
502 .x = (vm_int_t)VECMAT_FLOOR(v->x),
503 .y = (vm_int_t)VECMAT_FLOOR(v->y),
504 .z = (vm_int_t)VECMAT_FLOOR(v->z)
505 };
506}
507
515{
516 return (vector3i){
517 .x = (vm_int_t)VECMAT_ROUND(v->x),
518 .y = (vm_int_t)VECMAT_ROUND(v->y),
519 .z = (vm_int_t)VECMAT_ROUND(v->z)
520 };
521}
vm_float_t v[VECMAT_MAT2_SIZE]
Definition vecmat.h:213
vm_int_t v[VECMAT_MAT2_SIZE]
Definition vecmat.h:294
vm_float_t v[VECMAT_MAT3_SIZE]
Definition vecmat.h:238
vm_int_t v[VECMAT_MAT3_SIZE]
Definition vecmat.h:319
vm_float_t v[VECMAT_MAT4_SIZE]
Definition vecmat.h:271
vm_int_t v[VECMAT_MAT4_SIZE]
Definition vecmat.h:352
vm_float_t y
Definition vecmat.h:364
vm_float_t x
Definition vecmat.h:363
vm_float_t z
Definition vecmat.h:365
vm_float_t w
Definition vecmat.h:366
vm_float_t y
Definition vecmat.h:128
vm_float_t x
Definition vecmat.h:127
vm_int_t y
Definition vecmat.h:165
vm_int_t x
Definition vecmat.h:164
vm_float_t z
Definition vecmat.h:139
vm_float_t y
Definition vecmat.h:138
vm_float_t x
Definition vecmat.h:137
vm_int_t x
Definition vecmat.h:174
vm_int_t y
Definition vecmat.h:175
vm_int_t z
Definition vecmat.h:176
vm_float_t z
Definition vecmat.h:150
vm_float_t x
Definition vecmat.h:148
vm_float_t y
Definition vecmat.h:149
vm_float_t w
Definition vecmat.h:151
vm_int_t z
Definition vecmat.h:187
vm_int_t y
Definition vecmat.h:186
vm_int_t w
Definition vecmat.h:188
vm_int_t x
Definition vecmat.h:185
vector2i vec2i(const vm_int_t x, const vm_int_t y)
Constructs a vector2i from x and y.
Definition vecmat.c:280
bool mat2_eq(const matrix2 a, const matrix2 b)
Returns true if two matrices are equal within VECMAT_EPSILON.
Definition vecmat.c:115
void vec2i_assign(vector2i *dest, const vector2i *src)
Copies src into dest.
Definition vecmat.c:291
void vec2_assign_xy(vector2 *dest, const vm_float_t x, const vm_float_t y)
Assigns x and y to dest.
Definition vecmat.c:244
bool mat2i_eq(const matrix2i a, const matrix2i b)
Returns true if two matrices are exactly equal.
Definition vecmat.c:160
void vec3i_assign_xyz(vector3i *dest, const vm_int_t x, const vm_int_t y, const vm_int_t z)
Assigns x, y, and z to dest.
Definition vecmat.c:458
void vec2i_add_assign(vector2i *dest, const vector2i *src)
Adds src to dest in place.
Definition vecmat.c:316
bool vec3_eq(const vector3 a, const vector3 b)
Returns true if two vectors are equal within VECMAT_EPSILON.
Definition vecmat.c:50
vector3i vec3i_from(const vector3 *v)
Converts a vector3 to a vector3i by truncation.
Definition vecmat.c:484
bool vec4_eq(const vector4 a, const vector4 b)
Returns true if two vectors are equal within VECMAT_EPSILON.
Definition vecmat.c:64
vector3 vec3_from(const vector3i *v)
Converts a vector3i to a vector3.
Definition vecmat.c:415
bool vec3i_eq(const vector3i a, const vector3i b)
Returns true if two vectors are exactly equal.
Definition vecmat.c:91
vector2i vec2i_from_floored(const vector2 *v)
Converts a vector2 to a vector2i by flooring each component.
Definition vecmat.c:339
void vec3_assign_xyz(vector3 *dest, const vm_float_t x, const vm_float_t y, const vm_float_t z)
Assigns x, y, and z to dest.
Definition vecmat.c:389
vector2 vec2(const vm_float_t x, const vm_float_t y)
Constructs a vector2 from x and y.
Definition vecmat.c:220
bool mat4i_eq(const matrix4i a, const matrix4i b)
Returns true if two matrices are exactly equal.
Definition vecmat.c:190
void vec2_assign(vector2 *dest, const vector2 *src)
Copies src into dest.
Definition vecmat.c:231
void vec3_add_assign(vector3 *dest, const vector3 *src)
Adds src to dest in place.
Definition vecmat.c:402
vector2 vec2_from(const vector2i *v)
Converts a vector2i to a vector2.
Definition vecmat.c:268
vector3 vec3(const vm_float_t x, const vm_float_t y, const vm_float_t z)
Constructs a vector3 from x, y, and z.
Definition vecmat.c:363
bool vec2i_eq(const vector2i a, const vector2i b)
Returns true if two vectors are exactly equal.
Definition vecmat.c:79
bool mat4_eq(const matrix4 a, const matrix4 b)
Returns true if two matrices are equal within VECMAT_EPSILON.
Definition vecmat.c:145
void vec3i_assign(vector3i *dest, const vector3i *src)
Copies src into dest.
Definition vecmat.c:443
vector3i vec3i_from_floored(const vector3 *v)
Converts a vector3 to a vector3i by flooring each component.
Definition vecmat.c:499
void vec3i_add_assign(vector3i *dest, const vector3i *src)
Adds src to dest in place.
Definition vecmat.c:471
vector3i vec3i(const vm_int_t x, const vm_int_t y, const vm_int_t z)
Constructs a vector3i from x, y, and z.
Definition vecmat.c:432
bool quat_eq(const quaternion a, const quaternion b)
Returns true if two quaternions are equal within VECMAT_EPSILON.
Definition vecmat.c:205
bool mat3i_eq(const matrix3i a, const matrix3i b)
Returns true if two matrices are exactly equal.
Definition vecmat.c:175
bool vec4i_eq(const vector4i a, const vector4i b)
Returns true if two vectors are exactly equal.
Definition vecmat.c:103
void vec2_add_assign(vector2 *dest, const vector2 *src)
Adds src to dest in place.
Definition vecmat.c:256
vector2i vec2i_from_rounded(const vector2 *v)
Converts a vector2 to a vector2i by rounding each component.
Definition vecmat.c:350
vector3i vec3i_from_rounded(const vector3 *v)
Converts a vector3 to a vector3i by rounding each component.
Definition vecmat.c:514
vector2i vec2i_from(const vector2 *v)
Converts a vector2 to a vector2i by truncation.
Definition vecmat.c:328
void vec3_assign(vector3 *dest, const vector3 *src)
Copies src into dest.
Definition vecmat.c:374
bool mat3_eq(const matrix3 a, const matrix3 b)
Returns true if two matrices are equal within VECMAT_EPSILON.
Definition vecmat.c:130
void vec2i_assign_xy(vector2i *dest, const vm_int_t x, const vm_int_t y)
Assigns x and y to dest.
Definition vecmat.c:304
vm_float_t rad_to_deg(const vm_float_t radians)
Converts radians to degrees.
Definition vecmat.c:25
bool vec2_eq(const vector2 a, const vector2 b)
Returns true if two vectors are equal within VECMAT_EPSILON.
Definition vecmat.c:37
vm_float_t deg_to_rad(const vm_float_t degrees)
Converts degrees to radians.
Definition vecmat.c:14
static const vm_float_t VM_DEG_TO_RAD
Definition vecmat.h:85
#define VECMAT_EPSILON
Definition vecmat.h:377
#define VECMAT_FLOOR(x)
Definition vecmat.h:424
static const vm_float_t VM_RAD_TO_DEG
Definition vecmat.h:86
#define VECMAT_ROUND(x)
Definition vecmat.h:426
double vm_float_t
Definition vecmat.h:79
#define VECMAT_FABS(x)
Definition vecmat.h:413
int8_t vm_int_t
Definition vecmat.h:103