Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector2.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 (vector2){.x = 0.0f, .y = 0.0f};
15}
16
23{
24 return (vector2){.x = 1.0f, .y = 1.0f};
25}
26
34{
35 return (vector2){.x = x, .y = 0.0f};
36}
37
45{
46 return (vector2){.x = 0.0f, .y = y};
47}
48
56{
57 return (vector2){.x = x, .y = 1.0f};
58}
59
67{
68 return (vector2){.x = 1.0f, .y = y};
69}
70
81{
82 vector2 res;
83 vec2_scale_ptr(&res, &v, s);
84 return res;
85}
86
97{
98 vector2 res;
99 vec2_add_ptr(&res, &a, &b);
100 return res;
101}
102
113{
114 vector2 res;
115 vec2_sub_ptr(&res, &a, &b);
116 return res;
117}
118
129{
130 vector2 res;
131 vec2_mul_scalar_ptr(&res, &v, s);
132 return res;
133}
134
145{
146 vector2 res;
147 vec2_div_scalar_ptr(&res, &v, s);
148 return res;
149}
150
161{
162 vector2 res;
163 vec2_mul_ptr(&res, &a, &b);
164 return res;
165}
166
176{
177 vector2 res;
178 vec2_neg_ptr(&res, &v);
179 return res;
180}
181
191{
192 vector2 res;
193 vec2_abs_ptr(&res, &v);
194 return res;
195}
196
207{
208 vector2 res;
209 vec2_cross_ptr(&res, &a, &b);
210 return res;
211}
212
222{
223 vector2 res;
224 vec2_normalize_ptr(&res, &v);
225 return res;
226}
227
238{
239 vector2 res;
240 vec2_min_ptr(&res, &a, &b);
241 return res;
242}
243
254{
255 vector2 res;
256 vec2_max_ptr(&res, &a, &b);
257 return res;
258}
259
269{
270 vector2 res;
271 vec2_sign_ptr(&res, &v);
272 return res;
273}
274
284{
285 vector2 res;
286 vec2_floor_ptr(&res, &v);
287 return res;
288}
289
299{
300 vector2 res;
301 vec2_ceil_ptr(&res, &v);
302 return res;
303}
304
314{
315 vector2 res;
316 vec2_round_ptr(&res, &v);
317 return res;
318}
319
320
330{
331 vector2 res;
332 vec2_perpendicular_ptr(&res, &v);
333 return res;
334}
335
345vector2 vec2_reflect(const vector2 v, const vector2 normal)
346{
347 vector2 res;
348 vec2_reflect_ptr(&res, &v, &normal);
349 return res;
350}
351
362{
363 vector2 res;
364 vec2_project_ptr(&res, &a, &b);
365 return res;
366}
367
377{
378 vector2 res;
379 vec2_tangent_ptr(&res, &v);
380 return res;
381}
382
393{
394 vector2 res;
395 vec2_rotate_ptr(&res, &v, angle);
396 return res;
397}
398
408vector2 vec2_slide(const vector2 v, const vector2 normal)
409{
410 vector2 res;
411 vec2_slide_ptr(&res, &v, &normal);
412 return res;
413}
414
425vector2 vec2_clamp(const vector2 v, const vector2 min, const vector2 max)
426{
427 vector2 res;
428 vec2_clamp_ptr(&res, &v, &min, &max);
429 return res;
430}
431
440{
441 return a.x * b.x + a.y * b.y;
442}
443
451{
452 return VECMAT_SQRT(vec2_dot(v, v));
453}
454
464{
465 if (v.y == 0.0f) return 0.0f;
466 return v.x / v.y;
467}
468
477{
478 const vm_float_t dx = a.x - b.x;
479 const vm_float_t dy = a.y - b.y;
480 return VECMAT_SQRT(dx * dx + dy * dy);
481}
482
493{
494 const vm_float_t dot = vec2_dot(a, b);
495 const vm_float_t len_a = vec2_length(a);
496 const vm_float_t len_b = vec2_length(b);
497 if (len_a == 0.0f || len_b == 0.0f) return 0.0f;
498 return VECMAT_ACOS(dot / (len_a * len_b));
499}
500
511vector2 vec2_lerp(const vector2 a, const vector2 b, const vm_float_t t)
512{
513 vector2 res;
514 vec2_lerp_ptr(&res, &a, &b, t);
515 return res;
516}
517
528{
529 vector2 res;
530 vec2_div_ptr(&res, &a, &b);
531 return res;
532}
533
544{
545 vector2 res;
546 vec2_add_scalar_ptr(&res, &v, s);
547 return res;
548}
549
560{
561 vector2 res;
562 vec2_sub_scalar_ptr(&res, &v, s);
563 return res;
564}
565
577{
578 vector2 res;
579 vec2_clamp_scalar_ptr(&res, &v, min, max);
580 return res;
581}
582
592{
593 vector2 res;
594 vec2_saturate_ptr(&res, &v);
595 return res;
596}
597
607{
608 vector2 res;
609 vec2_fract_ptr(&res, &v);
610 return res;
611}
612
623vector2 vec2_refract(const vector2 incident, const vector2 normal, const vm_float_t eta)
624{
625 vector2 res;
626 vec2_refract_ptr(&res, &incident, &normal, eta);
627 return res;
628}
629
640{
641 vector2 res;
642 vec2_reject_ptr(&res, &a, &b);
643 return res;
644}
645
655{
656 return (vector2){.x = s, .y = s};
657}
658
668{
669 return (vector2){.x = VECMAT_COS(angle), .y = VECMAT_SIN(angle)};
670}
671
682vector2 vec2_rotate_around(const vector2 v, const vector2 pivot, const vm_float_t angle)
683{
684 vector2 res;
685 vec2_rotate_around_ptr(&res, &v, &pivot, angle);
686 return res;
687}
688
699vector2 vec2_move_toward(const vector2 current, const vector2 target, const vm_float_t max_delta)
700{
701 vector2 res;
702 vec2_move_toward_ptr(&res, &current, &target, max_delta);
703 return res;
704}
705
716{
717 vector2 res;
718 vec2_limit_length_ptr(&res, &v, max_len);
719 return res;
720}
721
732{
733 vector3 res;
734 vec2_to_vec3_ptr(&res, &v, z);
735 return res;
736}
737
745{
746 return vec2_dot(v, v);
747}
748
756{
757 return VECMAT_FABS(v.x) + VECMAT_FABS(v.y);
758}
759
770
779{
780 const vm_float_t dx = a.x - b.x;
781 const vm_float_t dy = a.y - b.y;
782 return dx * dx + dy * dy;
783}
784
793{
794 return a.x * b.y - a.y * b.x;
795}
796
804{
805 return VECMAT_ATAN2(v.y, v.x);
806}
807
815{
816 return VECMAT_FMIN(v.x, v.y);
817}
818
826{
827 return VECMAT_FMAX(v.x, v.y);
828}
829
837{
838 return v.x + v.y;
839}
840
848{
850}
851
859{
861}
862
871bool vec2_near(const vector2 a, const vector2 b, const vm_float_t eps)
872{
873 return VECMAT_FABS(a.x - b.x) < eps && VECMAT_FABS(a.y - b.y) < eps;
874}
vm_float_t y
Definition vecmat.h:128
vm_float_t x
Definition vecmat.h:127
#define VECMAT_FMAX(a, b)
Definition vecmat.h:423
#define VECMAT_EPSILON
Definition vecmat.h:377
void vec2_rotate_ptr(vector2 *result, const vector2 *v, vm_float_t angle)
Rotates the input vector counterclockwise by the given angle (radians).
void vec2_min_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Computes the component-wise minimum of vectors a and b, storing the result in res.
void vec2_clamp_ptr(vector2 *res, const vector2 *v, const vector2 *min, const vector2 *max)
Clamps vector v component-wise between min and max.
void vec2_perpendicular_ptr(vector2 *res, const vector2 *v)
Computes the perpendicular vector to v (90 degrees counterclockwise rotation), storing the result in ...
void vec2_move_toward_ptr(vector2 *res, const vector2 *current, const vector2 *target, vm_float_t max_delta)
Moves current toward target by at most max_delta.
void vec2_normalize_ptr(vector2 *res, const vector2 *v)
Normalizes vector v to unit length, storing the result in res.
void vec2_clamp_scalar_ptr(vector2 *res, const vector2 *v, vm_float_t min, vm_float_t max)
Clamps each component to the scalar range [min, max].
#define VECMAT_FMIN(a, b)
Definition vecmat.h:422
void vec2_limit_length_ptr(vector2 *res, const vector2 *v, vm_float_t max_len)
Clamps the vector length to max_len.
void vec2_mul_scalar_ptr(vector2 *res, const vector2 *v, vm_float_t s)
Multiplies vector v by scalar s component-wise, storing the result in res.
Definition vector2_ptr.c:40
void vec2_round_ptr(vector2 *res, const vector2 *v)
Applies the round function to each component of vector v, storing the result in res.
void vec2_tangent_ptr(vector2 *res, const vector2 *v)
Computes a tangent vector perpendicular to the input (90 degrees clockwise).
void vec2_div_scalar_ptr(vector2 *res, const vector2 *v, vm_float_t s)
Divides vector v by scalar s component-wise, storing the result in res.
Definition vector2_ptr.c:53
void vec2_ceil_ptr(vector2 *res, const vector2 *v)
Applies the ceil function to each component of vector v, storing the result in res.
#define VECMAT_ACOS(x)
Definition vecmat.h:419
void vec2_scale_ptr(vector2 *res, const vector2 *v, vm_float_t s)
Scales a vector by a scalar component-wise, storing the result in res.
void vec2_saturate_ptr(vector2 *res, const vector2 *v)
Clamps each component to the range [0, 1].
void vec2_abs_ptr(vector2 *res, const vector2 *v)
Computes the absolute values of the components of vector v, storing the result in res.
Definition vector2_ptr.c:95
void vec2_add_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Adds vectors a and b component-wise, storing the result in res.
Definition vector2_ptr.c:14
void vec2_reject_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Returns the component of a orthogonal to b.
void vec2_sign_ptr(vector2 *res, const vector2 *v)
Sets each component of res to the sign of the corresponding component in v (+1, -1).
void vec2_neg_ptr(vector2 *res, const vector2 *v)
Negates the components of vector v, storing the result in res.
Definition vector2_ptr.c:82
#define VECMAT_SIN(x)
Definition vecmat.h:415
void vec2_div_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Divides two vectors component-wise.
void vec2_rotate_around_ptr(vector2 *res, const vector2 *v, const vector2 *pivot, vm_float_t angle)
Rotates v around pivot by angle radians.
void vec2_project_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Projects vector a onto vector b (scalar projection scaled by b).
void vec2_reflect_ptr(vector2 *res, const vector2 *v, const vector2 *normal)
Reflects vector v across the normal, storing the result in res.
void vec2_max_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Computes the component-wise maximum of vectors a and b, storing the result in res.
void vec2_sub_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Subtracts vector b from vector a component-wise, storing the result in res.
Definition vector2_ptr.c:27
void vec2_slide_ptr(vector2 *result, const vector2 *v, const vector2 *normal)
Slides the input vector tangent to the normal (removes normal component).
void vec2_to_vec3_ptr(vector3 *res, const vector2 *v, vm_float_t z)
Converts a vector2 to a vector3 with the given z.
double vm_float_t
Definition vecmat.h:79
#define VECMAT_FABS(x)
Definition vecmat.h:413
void vec2_sub_scalar_ptr(vector2 *res, const vector2 *v, vm_float_t s)
Subtracts a scalar from each component.
void vec2_add_scalar_ptr(vector2 *res, const vector2 *v, vm_float_t s)
Adds a scalar to each component.
#define VECMAT_SQRT(x)
Definition vecmat.h:414
void vec2_refract_ptr(vector2 *res, const vector2 *incident, const vector2 *normal, vm_float_t eta)
Computes the refraction of incident across normal with ratio eta.
void vec2_mul_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Multiplies vectors a and b component-wise, storing the result in res.
Definition vector2_ptr.c:70
#define VECMAT_COS(x)
Definition vecmat.h:416
#define VECMAT_ATAN2(y, x)
Definition vecmat.h:421
void vec2_fract_ptr(vector2 *res, const vector2 *v)
Returns the fractional part of each component.
void vec2_lerp_ptr(vector2 *res, const vector2 *a, const vector2 *b, vm_float_t t)
Linearly interpolates from a to b by t.
void vec2_floor_ptr(vector2 *res, const vector2 *v)
Applies the floor function to each component of vector v, storing the result in res.
void vec2_cross_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Computes the 2D cross-product of a and b, storing the scalar value in res->x and 0 in res->y.
vm_float_t vec2_length_chebyshev(const vector2 v)
Returns the Chebyshev (L-inf) length.
Definition vector2.c:766
vm_float_t vec2_cross_scalar(const vector2 a, const vector2 b)
Returns the 2D cross product as a scalar (a.x*b.y - a.y*b.x).
Definition vector2.c:792
vm_float_t vec2_sum(const vector2 v)
Returns the sum of all components.
Definition vector2.c:836
vector2 vec2_min(const vector2 a, const vector2 b)
Returns the component-wise minimum of two vectors.
Definition vector2.c:237
vector2 vec2_slide(const vector2 v, const vector2 normal)
Slides the input vector tangent to the normal (removes normal component).
Definition vector2.c:408
vector2 vec2_project(const vector2 a, const vector2 b)
Projects the first vector onto the second.
Definition vector2.c:361
vector2 vec2_reflect(const vector2 v, const vector2 normal)
Reflects vector v across the normal, storing the result in res.
Definition vector2.c:345
vm_float_t vec2_distance_squared(const vector2 a, const vector2 b)
Returns the squared Euclidean distance between a and b.
Definition vector2.c:778
vector2 vec2_sign(const vector2 v)
Returns the sign of each component (+1.0f, -1.0f, or 0.0f).
Definition vector2.c:268
vector2 vec2_sub_scalar(const vector2 v, const vm_float_t s)
Subtracts a scalar from each component.
Definition vector2.c:559
vector2 vec2_x_scale(const vm_float_t x)
Returns a vector2 representing x-axis scaling (y = 1.0f).
Definition vector2.c:55
vector2 vec2_ceil(const vector2 v)
Applies ceil to each component.
Definition vector2.c:298
vm_float_t vec2_angle(const vector2 a, const vector2 b)
Computes the angle between two vectors (in radians, range [0, PI]).
Definition vector2.c:492
bool vec2_is_normalized(const vector2 v)
Returns true if the vector has unit length.
Definition vector2.c:858
vector2 vec2_rotate_around(const vector2 v, const vector2 pivot, const vm_float_t angle)
Rotates v around pivot by angle radians.
Definition vector2.c:682
vector2 vec2_sub(const vector2 a, const vector2 b)
Subtracts the second vector from the first component-wise.
Definition vector2.c:112
vector2 vec2_floor(const vector2 v)
Applies floor to each component.
Definition vector2.c:283
vm_float_t vec2_heading(const vector2 v)
Returns the heading angle of the vector in radians.
Definition vector2.c:803
vector2 vec2_move_toward(const vector2 current, const vector2 target, const vm_float_t max_delta)
Moves current toward target by at most max_delta.
Definition vector2.c:699
vector2 vec2_clamp(const vector2 v, const vector2 min, const vector2 max)
Clamps vector v component-wise between min and max.
Definition vector2.c:425
vector2 vec2_round(const vector2 v)
Applies round to each component.
Definition vector2.c:313
vector2 vec2_mul(const vector2 a, const vector2 b)
Multiplies two vectors component-wise (Hadamard product).
Definition vector2.c:160
vector2 vec2_from_angle(const vm_float_t angle)
Returns the unit vector at the given angle in radians.
Definition vector2.c:667
vm_float_t vec2_aspect_ratio(const vector2 v)
Computes the aspect ratio of the vector (x / y).
Definition vector2.c:463
vm_float_t vec2_length_manhattan(const vector2 v)
Returns the Manhattan (L1) length.
Definition vector2.c:755
vector2 vec2_limit_length(const vector2 v, const vm_float_t max_len)
Clamps the vector length to max_len.
Definition vector2.c:715
vector2 vec2_max(const vector2 a, const vector2 b)
Returns the component-wise maximum of two vectors.
Definition vector2.c:253
vector2 vec2_splat(const vm_float_t s)
Returns a vector with every component set to s.
Definition vector2.c:654
bool vec2_near(const vector2 a, const vector2 b, const vm_float_t eps)
Returns true if a and b are within eps of each other.
Definition vector2.c:871
vector2 vec2_add_scalar(const vector2 v, const vm_float_t s)
Adds a scalar to each component.
Definition vector2.c:543
vector2 vec2_refract(const vector2 incident, const vector2 normal, const vm_float_t eta)
Computes the refraction of incident across normal with ratio eta.
Definition vector2.c:623
vector2 vec2_one(void)
Returns a vector2 with both components set to 1.0f.
Definition vector2.c:22
bool vec2_is_zero(const vector2 v)
Returns true if every component is zero.
Definition vector2.c:847
vector2 vec2_clamp_scalar(const vector2 v, const vm_float_t min, const vm_float_t max)
Clamps each component to the scalar range [min, max].
Definition vector2.c:576
vm_float_t vec2_min_component(const vector2 v)
Returns the smallest component.
Definition vector2.c:814
vector2 vec2_cross(const vector2 a, const vector2 b)
Computes the 2D cross-product as a vector.
Definition vector2.c:206
vector2 vec2_y_scale(const vm_float_t y)
Returns a vector2 representing y-axis scaling (x = 1.0f).
Definition vector2.c:66
vector2 vec2_fract(const vector2 v)
Returns the fractional part of each component.
Definition vector2.c:606
vector3 vec2_to_vec3(const vector2 v, const vm_float_t z)
Converts a vector2 to a vector3 using z.
Definition vector2.c:731
vector2 vec2_reject(const vector2 a, const vector2 b)
Returns the component of a orthogonal to b.
Definition vector2.c:639
vector2 vec2_tangent(const vector2 v)
Returns the tangent vector perpendicular to the input (90 degrees clockwise).
Definition vector2.c:376
vector2 vec2_perpendicular(const vector2 v)
Returns the perpendicular vector (90 degrees counterclockwise).
Definition vector2.c:329
vector2 vec2_lerp(const vector2 a, const vector2 b, const vm_float_t t)
Linearly interpolates from a to b by t.
Definition vector2.c:511
vector2 vec2_scale(const vector2 v, const vm_float_t s)
Scales a vector by a scalar component-wise.
Definition vector2.c:80
vector2 vec2_div_scalar(const vector2 v, const vm_float_t s)
Divides a vector by a scalar component-wise.
Definition vector2.c:144
vm_float_t vec2_dot(const vector2 a, const vector2 b)
Computes the dot product of two vectors.
Definition vector2.c:439
vector2 vec2_add(const vector2 a, const vector2 b)
Adds two vectors component-wise.
Definition vector2.c:96
vm_float_t vec2_length_squared(const vector2 v)
Returns the squared Euclidean length.
Definition vector2.c:744
vm_float_t vec2_max_component(const vector2 v)
Returns the largest component.
Definition vector2.c:825
vector2 vec2_normalize(const vector2 v)
Normalizes the vector to unit length.
Definition vector2.c:221
vector2 vec2_saturate(const vector2 v)
Clamps each component to the range [0, 1].
Definition vector2.c:591
vector2 vec2_rotate(const vector2 v, const vm_float_t angle)
Rotates the input vector counterclockwise by the given angle (radians).
Definition vector2.c:392
vm_float_t vec2_length(const vector2 v)
Computes the length (magnitude) of the vector.
Definition vector2.c:450
vector2 vec2_neg(const vector2 v)
Negates the vector (multiplies each component by -1.0f).
Definition vector2.c:175
vector2 vec2_x_axis(const vm_float_t x)
Returns a vector2 along the x-axis (y = 0.0f).
Definition vector2.c:33
vector2 vec2_zero(void)
Returns a vector2 with both components set to 0.0f.
Definition vector2.c:12
vm_float_t vec2_distance(const vector2 a, const vector2 b)
Computes the Euclidean distance between two vectors (treated as points).
Definition vector2.c:476
vector2 vec2_abs(const vector2 v)
Returns the absolute values of each component.
Definition vector2.c:190
vector2 vec2_y_axis(const vm_float_t y)
Returns a vector2 along the y-axis (x = 0.0f).
Definition vector2.c:44
vector2 vec2_div(const vector2 a, const vector2 b)
Divides two vectors component-wise.
Definition vector2.c:527
vector2 vec2_mul_scalar(const vector2 v, const vm_float_t s)
Multiplies a vector by a scalar component-wise.
Definition vector2.c:128