Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector4i.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 return (vector4i){.x = 0, .y = 0, .z = 0, .w = 0};
15}
16
23{
24 return (vector4i){.x = 1, .y = 1, .z = 1, .w = 1};
25}
26
34{
35 return (vector4i){.x = x, .y = 0, .z = 0, .w = 0};
36}
37
45{
46 return (vector4i){.x = 0, .y = y, .z = 0, .w = 0};
47}
48
56 return (vector4i){.x = 0, .y = 0, .z = z, .w = 0};
57}
58
66{
67 return (vector4i){.x = 0, .y = 0, .z = 0, .w = w};
68}
69
77{
78 return (vector4i){.x = x, .y = 1, .z = 1, .w = 1};
79}
80
88{
89 return (vector4i){.x = 1, .y = y, .z = 1, .w = 1};
90}
91
99{
100 return (vector4i){.x = 1, .y = 1, .z = z, .w = 1};
101}
102
110{
111 return (vector4i){.x = 1, .y = 1, .z = 1, .w = w};
112}
113
124{
125 vector4i res;
126 vec4i_add_ptr(&res, &a, &b);
127 return res;
128}
129
140{
141 vector4i res;
142 vec4i_sub_ptr(&res, &a, &b);
143 return res;
144}
145
156{
157 vector4i res;
158 vec4i_mul_scalar_ptr(&res, &v, s);
159 return res;
160}
161
172{
173 vector4i res;
174 vec4i_div_scalar_ptr(&res, &v, s);
175 return res;
176}
177
188{
189 vector4i res;
190 vec4i_mul_ptr(&res, &a, &b);
191 return res;
192}
193
203{
204 vector4i res;
205 vec4i_neg_ptr(&res, &v);
206 return res;
207}
208
218{
219 vector4i res;
220 vec4i_abs_ptr(&res, &v);
221 return res;
222}
223
233{
234 vector4i res;
235 vec4i_normalize_ptr(&res, &v);
236 return res;
237}
238
249{
250 vector4i res;
251 vec4i_min_ptr(&res, &a, &b);
252 return res;
253}
254
265{
266 vector4i res;
267 vec4i_max_ptr(&res, &a, &b);
268 return res;
269}
270
280{
281 vector4i res;
282 vec4i_sign_ptr(&res, &v);
283 return res;
284}
285
297{
298 vector4i res;
299 vec4i_lerp_ptr(&res, &a, &b, t);
300 return res;
301}
302
314vector4i vec4i_clamp(const vector4i v, const vector4i min, const vector4i max)
315{
316 vector4i res;
317 vec4i_clamp_ptr(&res, &v, &min, &max);
318 return res;
319}
320
329{
330 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
331}
332
340{
341 return VECMAT_SQRT(vec4i_dot(v, v));
342}
343
352{
353 const vector4i diff = vec4i_sub(a, b);
354 return vec4i_length(diff);
355}
356
364{
365 vector3i res;
366 vec4i_to_vec3i_ptr(&res, &v);
367 return res;
368}
369
380{
381 vector4i res;
382 vec4i_div_ptr(&res, &a, &b);
383 return res;
384}
385
396{
397 vector4i res;
398 vec4i_add_scalar_ptr(&res, &v, s);
399 return res;
400}
401
412{
413 vector4i res;
414 vec4i_sub_scalar_ptr(&res, &v, s);
415 return res;
416}
417
428{
429 vector4i res;
430 vec4i_mod_ptr(&res, &a, &b);
431 return res;
432}
433
444{
445 vector4i res;
446 vec4i_div_floor_ptr(&res, &a, &b);
447 return res;
448}
449
459vector4i vec4i_wrap(const vector4i v, const vector4i period)
460{
461 vector4i res;
462 vec4i_wrap_ptr(&res, &v, &period);
463 return res;
464}
465
475{
476 return (vector4i){.x = s, .y = s, .z = s, .w = s};
477}
478
486{
487 return vec4i_dot(v, v);
488}
489
498{
499 return vec4i_length_squared(vec4i_sub(a, b));
500}
501
509{
510 const vm_int_t ax = v.x < 0 ? -v.x : v.x;
511 const vm_int_t ay = v.y < 0 ? -v.y : v.y;
512 const vm_int_t az = v.z < 0 ? -v.z : v.z;
513 const vm_int_t aw = v.w < 0 ? -v.w : v.w;
514 return ax + ay + az + aw;
515}
516
524{
525 vm_int_t m = v.x < 0 ? -v.x : v.x;
526 const vm_int_t ay = v.y < 0 ? -v.y : v.y;
527 const vm_int_t az = v.z < 0 ? -v.z : v.z;
528 const vm_int_t aw = v.w < 0 ? -v.w : v.w;
529 if (ay > m) m = ay;
530 if (az > m) m = az;
531 if (aw > m) m = aw;
532 return m;
533}
534
542{
543 vm_int_t m = v.x;
544 if (v.y < m) m = v.y;
545 if (v.z < m) m = v.z;
546 if (v.w < m) m = v.w;
547 return m;
548}
549
557{
558 vm_int_t m = v.x;
559 if (v.y > m) m = v.y;
560 if (v.z > m) m = v.z;
561 if (v.w > m) m = v.w;
562 return m;
563}
564
572{
573 return v.x + v.y + v.z + v.w;
574}
575
583{
584 return v.x == 0 && v.y == 0 && v.z == 0 && v.w == 0;
585}
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
void vec4i_div_scalar_ptr(vector4i *res, const vector4i *v, vm_int_t s)
Divides the components of a vector4i by a scalar and stores the result in res.
void vec4i_lerp_ptr(vector4i *res, const vector4i *a, const vector4i *b, vm_float_t t)
Linearly interpolates between two vector4i using an interpolation factor t (clamped to [0,...
void vec4i_add_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Adds the components of two vector4i and stores the result in res.
void vec4i_to_vec3i_ptr(vector3i *res, const vector4i *v)
Copies the x, y, z components of a vector4i and stores the result in a vector3i.
void vec4i_div_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Divides two vectors component-wise.
void vec4i_mod_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Component-wise floor modulo of a by b.
void vec4i_sub_scalar_ptr(vector4i *res, const vector4i *v, vm_int_t s)
Subtracts a scalar from each component.
void vec4i_abs_ptr(vector4i *res, const vector4i *v)
Computes the absolute value of each component of a vector4i and stores the result in res.
void vec4i_clamp_ptr(vector4i *res, const vector4i *v, const vector4i *min, const vector4i *max)
Clamps the components of a vector4i to the range [min, max] and stores the result in res.
void vec4i_mul_scalar_ptr(vector4i *res, const vector4i *v, vm_int_t s)
Multiplies the components of a vector4i by a scalar and stores the result in res.
void vec4i_max_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Computes the component-wise maximum of two vector4i and stores the result in res.
void vec4i_normalize_ptr(vector4i *res, const vector4i *v)
Normalizes a vector4i by dividing its components by the vector length and stores the result in res.
void vec4i_mul_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Multiplies the components of two vector4i and stores the result in res.
void vec4i_add_scalar_ptr(vector4i *res, const vector4i *v, vm_int_t s)
Adds a scalar to each component.
void vec4i_neg_ptr(vector4i *res, const vector4i *v)
Negates the components of a vector4i and stores the result in res.
void vec4i_min_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Takes the component-wise minimum of two vector4i and stores the result in res.
void vec4i_sub_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Subtracts the components of the second vector from the first vector4i and stores the result in res.
double vm_float_t
Definition vecmat.h:79
#define VECMAT_SQRT(x)
Definition vecmat.h:414
void vec4i_div_floor_ptr(vector4i *res, const vector4i *a, const vector4i *b)
Component-wise floored division of a by b.
int8_t vm_int_t
Definition vecmat.h:103
void vec4i_sign_ptr(vector4i *res, const vector4i *v)
Computes the sign (-1, 0, or 1) of each component of a vector4i and stores the result in res.
void vec4i_wrap_ptr(vector4i *res, const vector4i *v, const vector4i *period)
Wraps each component of v into [0, period).
vector4i vec4i_add_scalar(const vector4i v, const vm_int_t s)
Adds a scalar to each component.
Definition vector4i.c:395
bool vec4i_is_zero(const vector4i v)
Returns true if every component is zero.
Definition vector4i.c:582
vector4i vec4i_lerp(const vector4i a, const vector4i b, const vm_float_t t)
Linearly interpolates between two vector4i.
Definition vector4i.c:296
vector4i vec4i_z_scale(const vm_int_t z)
Returns a vector4i for scaling along the z-axis.
Definition vector4i.c:98
vm_float_t vec4i_length(const vector4i v)
Computes the length (magnitude) of a vector4i.
Definition vector4i.c:339
vector4i vec4i_y_axis(const vm_int_t y)
Returns a vector4i along the y-axis.
Definition vector4i.c:44
vector4i vec4i_splat(const vm_int_t s)
Returns a vector with every component set to s.
Definition vector4i.c:474
vector4i vec4i_w_axis(const vm_int_t w)
Returns a vector4i along the w-axis.
Definition vector4i.c:65
vector4i vec4i_div_scalar(const vector4i v, const vm_int_t s)
Component-wise division of vector by scalar.
Definition vector4i.c:171
vm_int_t vec4i_length_squared(const vector4i v)
Returns the squared Euclidean length.
Definition vector4i.c:485
vector4i vec4i_w_scale(const vm_int_t w)
Returns a vector4i for scaling along the w-axis.
Definition vector4i.c:109
vector4i vec4i_div(const vector4i a, const vector4i b)
Divides two vectors component-wise.
Definition vector4i.c:379
vm_int_t vec4i_length_chebyshev(const vector4i v)
Returns the Chebyshev (L-inf) length.
Definition vector4i.c:523
vector4i vec4i_x_scale(const vm_int_t x)
Returns a vector4i for scaling along the x-axis.
Definition vector4i.c:76
vector4i vec4i_max(const vector4i a, const vector4i b)
Computes the component-wise maximum of two vector4i.
Definition vector4i.c:264
vector4i vec4i_clamp(const vector4i v, const vector4i min, const vector4i max)
Clamps each component of the input vector to the range defined by the minimum and maximum vectors.
Definition vector4i.c:314
vector4i vec4i_one(void)
Returns a vector4i with all components set to 1.
Definition vector4i.c:22
vm_int_t vec4i_distance_squared(const vector4i a, const vector4i b)
Returns the squared Euclidean distance between a and b.
Definition vector4i.c:497
vector4i vec4i_sign(const vector4i v)
Computes the sign per component of a vector4i (-1, 0, or 1).
Definition vector4i.c:279
vm_int_t vec4i_length_manhattan(const vector4i v)
Returns the Manhattan (L1) length.
Definition vector4i.c:508
vector4i vec4i_min(const vector4i a, const vector4i b)
Computes the component-wise minimum of two vector4i.
Definition vector4i.c:248
vector3i vec4i_to_vec3i(const vector4i v)
Converts a vector4i to a vector3i (discards the w component).
Definition vector4i.c:363
vm_float_t vec4i_distance(const vector4i a, const vector4i b)
Computes the Euclidean distance between two vector4i.
Definition vector4i.c:351
vector4i vec4i_abs(const vector4i v)
Computes the absolute value per component of a vector4i.
Definition vector4i.c:217
vector4i vec4i_mul_scalar(const vector4i v, const vm_int_t s)
Component-wise multiplication of vector by scalar.
Definition vector4i.c:155
vector4i vec4i_div_floor(const vector4i a, const vector4i b)
Component-wise floored division of a by b.
Definition vector4i.c:443
vector4i vec4i_neg(const vector4i v)
Negation of a vector.
Definition vector4i.c:202
vm_int_t vec4i_min_component(const vector4i v)
Returns the smallest component.
Definition vector4i.c:541
vector4i vec4i_x_axis(const vm_int_t x)
Returns a vector4i along the x-axis.
Definition vector4i.c:33
vector4i vec4i_mul(const vector4i a, const vector4i b)
Component-wise multiplication of two vectors.
Definition vector4i.c:187
vector4i vec4i_sub_scalar(const vector4i v, const vm_int_t s)
Subtracts a scalar from each component.
Definition vector4i.c:411
vector4i vec4i_wrap(const vector4i v, const vector4i period)
Wraps each component of v into [0, period).
Definition vector4i.c:459
vm_int_t vec4i_max_component(const vector4i v)
Returns the largest component.
Definition vector4i.c:556
vector4i vec4i_add(const vector4i a, const vector4i b)
Component-wise addition of two vectors.
Definition vector4i.c:123
vector4i vec4i_y_scale(const vm_int_t y)
Returns a vector4i for scaling along the y-axis.
Definition vector4i.c:87
vector4i vec4i_mod(const vector4i a, const vector4i b)
Component-wise floor modulo of a by b.
Definition vector4i.c:427
vector4i vec4i_zero(void)
Returns a zero-initialized vector4i.
Definition vector4i.c:12
vm_int_t vec4i_sum(const vector4i v)
Returns the sum of all components.
Definition vector4i.c:571
vector4i vec4i_z_axis(const vm_int_t z)
Returns a vector4i along the z-axis.
Definition vector4i.c:55
vm_int_t vec4i_dot(const vector4i a, const vector4i b)
Computes the dot product of two vector4i.
Definition vector4i.c:328
vector4i vec4i_sub(const vector4i a, const vector4i b)
Component-wise subtraction of two vectors.
Definition vector4i.c:139
vector4i vec4i_normalize(const vector4i v)
Normalizes a vector4i to unit length.
Definition vector4i.c:232