Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector4.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 (vector4){.x = 0.0f, .y = 0.0f, .z = 0.0f, .w = 0.0f};
15}
16
23{
24 return (vector4){.x = 1.0f, .y = 1.0f, .z = 1.0f, .w = 1.0f};
25}
26
34{
35 return (vector4){.x = x, .y = 0.0f, .z = 0.0f, .w = 0.0f};
36}
37
45{
46 return (vector4){.x = 0.0f, .y = y, .z = 0.0f, .w = 0.0f};
47}
48
56{
57 return (vector4){.x = 0.0f, .y = 0.0f, .z = z, .w = 0.0f};
58}
59
67{
68 return (vector4){.x = 0.0f, .y = 0.0f, .z = 0.0f, .w = w};
69}
70
78{
79 return (vector4){.x = x, .y = 1.0f, .z = 1.0f, .w = 1.0f};
80}
81
89{
90 return (vector4){.x = 1.0f, .y = y, .z = 1.0f, .w = 1.0f};
91}
92
100{
101 return (vector4){.x = 1.0f, .y = 1.0f, .z = z, .w = 1.0f};
102}
103
111{
112 return (vector4){.x = 1.0f, .y = 1.0f, .z = 1.0f, .w = w};
113}
114
125{
126 vector4 res;
127 vec4_add_ptr(&res, &a, &b);
128 return res;
129}
130
141{
142 vector4 res;
143 vec4_sub_ptr(&res, &a, &b);
144 return res;
145}
146
157{
158 vector4 res;
159 vec4_mul_scalar_ptr(&res, &v, s);
160 return res;
161}
162
173{
174 vector4 res;
175 vec4_div_scalar_ptr(&res, &v, s);
176 return res;
177}
178
189{
190 vector4 res;
191 vec4_mul_ptr(&res, &a, &b);
192 return res;
193}
194
204{
205 vector4 res;
206 vec4_neg_ptr(&res, &v);
207 return res;
208}
209
219{
220 vector4 res;
221 vec4_abs_ptr(&res, &v);
222 return res;
223}
224
234{
235 vector4 res;
236 vec4_normalize_ptr(&res, &v);
237 return res;
238}
239
250{
251 vector4 res;
252 vec4_min_ptr(&res, &a, &b);
253 return res;
254}
255
266{
267 vector4 res;
268 vec4_max_ptr(&res, &a, &b);
269 return res;
270}
271
281{
282 vector4 res;
283 vec4_sign_ptr(&res, &v);
284 return res;
285}
286
296{
297 vector4 res;
298 vec4_floor_ptr(&res, &v);
299 return res;
300}
301
311{
312 vector4 res;
313 vec4_ceil_ptr(&res, &v);
314 return res;
315}
316
326{
327 vector4 res;
328 vec4_round_ptr(&res, &v);
329 return res;
330}
331
342vector4 vec4_lerp(const vector4 a, const vector4 b, const vm_float_t t)
343{
344 vector4 res;
345 vec4_lerp_ptr(&res, &a, &b, t);
346 return res;
347}
348
359vector4 vec4_clamp(const vector4 v, const vector4 min, const vector4 max)
360{
361 vector4 res;
362 vec4_clamp_ptr(&res, &v, &min, &max);
363 return res;
364}
365
375{
376 vector4 res;
377 vec4_homogenize_ptr(&res, &v);
378 return res;
379}
380
389{
390 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
391}
392
400{
401 return VECMAT_SQRT(vec4_dot(v, v));
402}
403
412{
413 const vector4 diff = vec4_sub(a, b);
414 return vec4_length(diff);
415}
416
424{
425 vector3 res;
426 vec4_to_vec3_ptr(&res, &v);
427 return res;
428}
429
440{
441 vector4 res;
442 vec4_div_ptr(&res, &a, &b);
443 return res;
444}
445
456{
457 vector4 res;
458 vec4_add_scalar_ptr(&res, &v, s);
459 return res;
460}
461
472{
473 vector4 res;
474 vec4_sub_scalar_ptr(&res, &v, s);
475 return res;
476}
477
489{
490 vector4 res;
491 vec4_clamp_scalar_ptr(&res, &v, min, max);
492 return res;
493}
494
504{
505 vector4 res;
506 vec4_saturate_ptr(&res, &v);
507 return res;
508}
509
519{
520 vector4 res;
521 vec4_fract_ptr(&res, &v);
522 return res;
523}
524
535{
536 vector4 res;
537 vec4_project_ptr(&res, &a, &b);
538 return res;
539}
540
551{
552 vector4 res;
553 vec4_reject_ptr(&res, &a, &b);
554 return res;
555}
556
566vector4 vec4_slide(const vector4 v, const vector4 normal)
567{
568 vector4 res;
569 vec4_slide_ptr(&res, &v, &normal);
570 return res;
571}
572
582{
583 return (vector4){.x = s, .y = s, .z = s, .w = s};
584}
585
593{
594 return vec4_dot(v, v);
595}
596
605{
606 return vec4_length_squared(vec4_sub(a, b));
607}
608
616{
618}
619
627{
629}
630
639bool vec4_near(const vector4 a, const vector4 b, const vm_float_t eps)
640{
641 return VECMAT_FABS(a.x - b.x) <= eps
642 && VECMAT_FABS(a.y - b.y) <= eps
643 && VECMAT_FABS(a.z - b.z) <= eps
644 && VECMAT_FABS(a.w - b.w) <= eps;
645}
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
#define VECMAT_EPSILON
Definition vecmat.h:377
void vec4_to_vec3_ptr(vector3 *res, const vector4 *v)
Copies the x, y, z components from a vector4 to a vector3.
void vec4_sub_scalar_ptr(vector4 *res, const vector4 *v, vm_float_t s)
void vec4_div_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_min_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_floor_ptr(vector4 *res, const vector4 *v)
void vec4_sign_ptr(vector4 *res, const vector4 *v)
void vec4_lerp_ptr(vector4 *res, const vector4 *a, const vector4 *b, vm_float_t t)
void vec4_homogenize_ptr(vector4 *res, const vector4 *v)
void vec4_add_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_max_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_fract_ptr(vector4 *res, const vector4 *v)
void vec4_add_scalar_ptr(vector4 *res, const vector4 *v, vm_float_t s)
void vec4_abs_ptr(vector4 *res, const vector4 *v)
void vec4_saturate_ptr(vector4 *res, const vector4 *v)
void vec4_div_scalar_ptr(vector4 *res, const vector4 *v, vm_float_t s)
void vec4_mul_scalar_ptr(vector4 *res, const vector4 *v, vm_float_t s)
void vec4_clamp_scalar_ptr(vector4 *res, const vector4 *v, vm_float_t min, vm_float_t max)
void vec4_project_ptr(vector4 *res, const vector4 *a, const vector4 *b)
Projects a onto b.
void vec4_ceil_ptr(vector4 *res, const vector4 *v)
void vec4_reject_ptr(vector4 *res, const vector4 *a, const vector4 *b)
Returns the component of a orthogonal to b.
double vm_float_t
Definition vecmat.h:79
void vec4_round_ptr(vector4 *res, const vector4 *v)
#define VECMAT_FABS(x)
Definition vecmat.h:413
#define VECMAT_SQRT(x)
Definition vecmat.h:414
void vec4_neg_ptr(vector4 *res, const vector4 *v)
void vec4_normalize_ptr(vector4 *res, const vector4 *v)
void vec4_slide_ptr(vector4 *res, const vector4 *v, const vector4 *normal)
Removes the component of v along normal.
void vec4_mul_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_sub_ptr(vector4 *res, const vector4 *a, const vector4 *b)
void vec4_clamp_ptr(vector4 *res, const vector4 *v, const vector4 *min, const vector4 *max)
vector4 vec4_add_scalar(const vector4 v, const vm_float_t s)
Adds a scalar to each component.
Definition vector4.c:455
vector4 vec4_saturate(const vector4 v)
Clamps each component to the range [0, 1].
Definition vector4.c:503
vector3 vec4_to_vec3(const vector4 v)
Converts a vector4 to a vector3 (discards the w component).
Definition vector4.c:423
vector4 vec4_slide(const vector4 v, const vector4 normal)
Removes the component of v along normal.
Definition vector4.c:566
vector4 vec4_lerp(const vector4 a, const vector4 b, const vm_float_t t)
Linearly interpolates between two vector4.
Definition vector4.c:342
vector4 vec4_neg(const vector4 v)
Negation of a vector.
Definition vector4.c:203
vector4 vec4_abs(const vector4 v)
Computes the absolute value per component of a vector4.
Definition vector4.c:218
vector4 vec4_one(void)
Returns a vector4 with all components set to 1.0f.
Definition vector4.c:22
vector4 vec4_z_axis(const vm_float_t z)
Returns a vector4 along the z-axis.
Definition vector4.c:55
vm_float_t vec4_length(const vector4 v)
Computes the length (magnitude) of a vector4.
Definition vector4.c:399
vector4 vec4_z_scale(const vm_float_t z)
Returns a vector4for scaling along the z-axis.
Definition vector4.c:99
vector4 vec4_zero(void)
Returns a zero-initialized vector4.
Definition vector4.c:12
vector4 vec4_w_axis(const vm_float_t w)
Returns a vector4 along the w-axis.
Definition vector4.c:66
vector4 vec4_x_scale(const vm_float_t x)
Returns avector4 for scaling along the x-axis.
Definition vector4.c:77
vector4 vec4_add(const vector4 a, const vector4 b)
Component-wise addition of two vectors.
Definition vector4.c:124
vm_float_t vec4_dot(const vector4 a, const vector4 b)
Computes the dot product of two vector4.
Definition vector4.c:388
vector4 vec4_y_axis(const vm_float_t y)
Returns a vector4 along the y-axis.
Definition vector4.c:44
vector4 vec4_ceil(const vector4 v)
Applies ceil per component to a vector4.
Definition vector4.c:310
vm_float_t vec4_distance_squared(const vector4 a, const vector4 b)
Returns the squared Euclidean distance between a and b.
Definition vector4.c:604
bool vec4_near(const vector4 a, const vector4 b, const vm_float_t eps)
Returns true if a and b are within eps of each other.
Definition vector4.c:639
vector4 vec4_w_scale(const vm_float_t w)
Returns a vector4 for scaling along the w-axis.
Definition vector4.c:110
vector4 vec4_max(const vector4 a, const vector4 b)
Computes the component-wise maximum of two vector4.
Definition vector4.c:265
vector4 vec4_div_scalar(const vector4 v, const vm_float_t s)
Component-wise division of vector by scalar.
Definition vector4.c:172
vm_float_t vec4_distance(const vector4 a, const vector4 b)
Computes the Euclidean distance between two vector4.
Definition vector4.c:411
vector4 vec4_homogenize(const vector4 v)
Homogenizes a vector4 (divides x, y, z by w).
Definition vector4.c:374
vector4 vec4_mul_scalar(const vector4 v, const vm_float_t s)
Component-wise multiplication of vector by scalar.
Definition vector4.c:156
vector4 vec4_min(const vector4 a, const vector4 b)
Computes the component-wise minimum of two vector4.
Definition vector4.c:249
vector4 vec4_splat(const vm_float_t s)
Returns a vector with every component set to s.
Definition vector4.c:581
vector4 vec4_project(const vector4 a, const vector4 b)
Projects a onto b.
Definition vector4.c:534
vm_float_t vec4_length_squared(const vector4 v)
Returns the squared Euclidean length.
Definition vector4.c:592
vector4 vec4_sub(const vector4 a, const vector4 b)
Component-wise subtraction of two vectors.
Definition vector4.c:140
bool vec4_is_normalized(const vector4 v)
Returns true if the vector has unit length.
Definition vector4.c:626
vector4 vec4_x_axis(const vm_float_t x)
Returns a vector4 along the x-axis.
Definition vector4.c:33
vector4 vec4_clamp_scalar(const vector4 v, const vm_float_t min, const vm_float_t max)
Clamps each component to the scalar range [min, max].
Definition vector4.c:488
vector4 vec4_normalize(const vector4 v)
Normalizes a vector4 to unit length.
Definition vector4.c:233
vector4 vec4_floor(const vector4 v)
Applies the floor per component to a vector4.
Definition vector4.c:295
vector4 vec4_round(const vector4 v)
Applies round per component to a vector4.
Definition vector4.c:325
vector4 vec4_fract(const vector4 v)
Returns the fractional part of each component.
Definition vector4.c:518
bool vec4_is_zero(const vector4 v)
Returns true if every component is zero.
Definition vector4.c:615
vector4 vec4_div(const vector4 a, const vector4 b)
Divides two vectors component-wise.
Definition vector4.c:439
vector4 vec4_sign(const vector4 v)
Computes the sign per component of a vector4 (-1, 0, or 1).
Definition vector4.c:280
vector4 vec4_clamp(const vector4 v, const vector4 min, const vector4 max)
Clamps a vector4 between min and max per component.
Definition vector4.c:359
vector4 vec4_reject(const vector4 a, const vector4 b)
Returns the component of a orthogonal to b.
Definition vector4.c:550
vector4 vec4_y_scale(const vm_float_t y)
Returns a vector4 for scaling along the y-axis.
Definition vector4.c:88
vector4 vec4_sub_scalar(const vector4 v, const vm_float_t s)
Subtracts a scalar from each component.
Definition vector4.c:471
vector4 vec4_mul(const vector4 a, const vector4 b)
Component-wise multiplication of two vectors.
Definition vector4.c:188