Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector2_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
14void vec2_add_ptr(vector2 *res, const vector2 *a, const vector2 *b)
15{
16 res->x = a->x + b->x;
17 res->y = a->y + b->y;
18}
19
27void vec2_sub_ptr(vector2 *res, const vector2 *a, const vector2 *b)
28{
29 res->x = a->x - b->x;
30 res->y = a->y - b->y;
31}
32
40void vec2_mul_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
41{
42 res->x = v->x * s;
43 res->y = v->y * s;
44}
45
53void vec2_div_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
54{
55 if (s == 0.0f) {
56 *res = *v;
57 return;
58 }
59 res->x = v->x / s;
60 res->y = v->y / s;
61}
62
70void vec2_mul_ptr(vector2 *res, const vector2 *a, const vector2 *b)
71{
72 res->x = a->x * b->x;
73 res->y = a->y * b->y;
74}
75
82void vec2_neg_ptr(vector2 *res, const vector2 *v)
83{
84 res->x = -v->x;
85 res->y = -v->y;
86}
87
95void vec2_abs_ptr(vector2 *res, const vector2 *v)
96{
97 res->x = VECMAT_FABS(v->x);
98 res->y = VECMAT_FABS(v->y);
99}
100
108{
109 const vm_float_t len = vec2_length(*v);
110 if (len == 0.0f) {
111 *res = *v;
112 return;
113 }
114 const vm_float_t inv_len = 1.0f / len;
115 res->x = v->x * inv_len;
116 res->y = v->y * inv_len;
117}
118
127void vec2_min_ptr(vector2 *res, const vector2 *a, const vector2 *b)
128{
129 res->x = VECMAT_FMIN(a->x, b->x);
130 res->y = VECMAT_FMIN(a->y, b->y);
131}
132
141void vec2_max_ptr(vector2 *res, const vector2 *a, const vector2 *b)
142{
143 res->x = VECMAT_FMAX(a->x, b->x);
144 res->y = VECMAT_FMAX(a->y, b->y);
145}
146
154void vec2_sign_ptr(vector2 *res, const vector2 *v)
155{
156 res->x = VECMAT_COPYSIGN(1.0f, v->x);
157 res->y = VECMAT_COPYSIGN(1.0f, v->y);
158}
159
167void vec2_floor_ptr(vector2 *res, const vector2 *v)
168{
169 res->x = VECMAT_FLOOR(v->x);
170 res->y = VECMAT_FLOOR(v->y);
171}
172
180void vec2_ceil_ptr(vector2 *res, const vector2 *v)
181{
182 res->x = VECMAT_CEIL(v->x);
183 res->y = VECMAT_CEIL(v->y);
184}
185
193void vec2_round_ptr(vector2 *res, const vector2 *v)
194{
195 res->x = VECMAT_ROUND(v->x);
196 res->y = VECMAT_ROUND(v->y);
197}
198
207{
208 res->x = -v->y;
209 res->y = v->x;
210}
211
220void vec2_cross_ptr(vector2 *res, const vector2 *a, const vector2 *b)
221{
222
223 res->x = a->x * b->y - a->y * b->x;
224 res->y = 0.f;
225}
226
234void vec2_scale_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
235{
236 res->x = v->x * s;
237 res->y = v->y * s;
238}
239
247void vec2_reflect_ptr(vector2 *res, const vector2 *v, const vector2 *normal)
248{
249 const vm_float_t d = 2.0f * vec2_dot(*v, *normal);
250 res->x = v->x - normal->x * d;
251 res->y = v->y - normal->y * d;
252}
253
261void vec2_project_ptr(vector2 *res, const vector2 *a, const vector2 *b)
262{
263 const vm_float_t denom = vec2_dot(*b, *b);
264 if (denom == 0.0f) {
265 res->x = 0.0f;
266 res->y = 0.0f;
267 return;
268 }
269 const vm_float_t scale = vec2_dot(*a, *b) / denom;
270 res->x = b->x * scale;
271 res->y = b->y * scale;
272}
273
282void vec2_tangent_ptr(vector2 *res, const vector2 *v)
283{
284 res->x = v->y;
285 res->y = -v->x;
286}
287
297void vec2_rotate_ptr(vector2 *result, const vector2 *v, const vm_float_t angle)
298{
299 const vm_float_t cs = VECMAT_COS(angle);
300 const vm_float_t sn = VECMAT_SIN(angle);
301 const vm_float_t x = v->x;
302 const vm_float_t y = v->y;
303 result->x = x * cs - y * sn;
304 result->y = x * sn + y * cs;
305}
306
316void vec2_slide_ptr(vector2 *result, const vector2 *v, const vector2 *normal)
317{
318 const vm_float_t d = vec2_dot(*v, *normal);
319 result->x = v->x - normal->x * d;
320 result->y = v->y - normal->y * d;
321}
322
331void vec2_clamp_ptr(vector2 *res, const vector2 *v, const vector2 *min, const vector2 *max)
332{
333 res->x = VECMAT_FMIN(VECMAT_FMAX(v->x, min->x), max->x);
334 res->y = VECMAT_FMIN(VECMAT_FMAX(v->y, min->y), max->y);
335}
336
345void vec2_lerp_ptr(vector2 *res, const vector2 *a, const vector2 *b, const vm_float_t t)
346{
347 res->x = a->x + t * (b->x - a->x);
348 res->y = a->y + t * (b->y - a->y);
349}
350
358void vec2_div_ptr(vector2 *res, const vector2 *a, const vector2 *b)
359{
360 res->x = (b->x == 0.0f) ? 0.0f : a->x / b->x;
361 res->y = (b->y == 0.0f) ? 0.0f : a->y / b->y;
362}
363
371void vec2_add_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
372{
373 res->x = v->x + s;
374 res->y = v->y + s;
375}
376
384void vec2_sub_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
385{
386 res->x = v->x - s;
387 res->y = v->y - s;
388}
389
398void vec2_clamp_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t min, const vm_float_t max)
399{
400 res->x = VECMAT_FMIN(VECMAT_FMAX(v->x, min), max);
401 res->y = VECMAT_FMIN(VECMAT_FMAX(v->y, min), max);
402}
403
411{
412 vec2_clamp_scalar_ptr(res, v, 0.0f, 1.0f);
413}
414
421void vec2_fract_ptr(vector2 *res, const vector2 *v)
422{
423 res->x = v->x - VECMAT_FLOOR(v->x);
424 res->y = v->y - VECMAT_FLOOR(v->y);
425}
426
435void vec2_refract_ptr(vector2 *res, const vector2 *incident, const vector2 *normal, const vm_float_t eta)
436{
437 const vm_float_t dot = vec2_dot(*incident, *normal);
438 const vm_float_t k = 1.0f - eta * eta * (1.0f - dot * dot);
439 if (k < 0.0f) {
440 res->x = 0.0f;
441 res->y = 0.0f;
442 return;
443 }
444 const vm_float_t factor = eta * dot + VECMAT_SQRT(k);
445 res->x = eta * incident->x - factor * normal->x;
446 res->y = eta * incident->y - factor * normal->y;
447}
448
456void vec2_reject_ptr(vector2 *res, const vector2 *a, const vector2 *b)
457{
458 vector2 projected;
459 vec2_project_ptr(&projected, a, b);
460 res->x = a->x - projected.x;
461 res->y = a->y - projected.y;
462}
463
472void vec2_rotate_around_ptr(vector2 *res, const vector2 *v, const vector2 *pivot, const vm_float_t angle)
473{
474 vector2 offset;
475 offset.x = v->x - pivot->x;
476 offset.y = v->y - pivot->y;
477 vec2_rotate_ptr(res, &offset, angle);
478 res->x += pivot->x;
479 res->y += pivot->y;
480}
481
490void vec2_move_toward_ptr(vector2 *res, const vector2 *current, const vector2 *target, const vm_float_t max_delta)
491{
492 const vm_float_t dx = target->x - current->x;
493 const vm_float_t dy = target->y - current->y;
494 const vm_float_t dist = VECMAT_SQRT(dx * dx + dy * dy);
495 if (dist <= max_delta || dist == 0.0f) {
496 *res = *target;
497 return;
498 }
499 const vm_float_t scale = max_delta / dist;
500 res->x = current->x + dx * scale;
501 res->y = current->y + dy * scale;
502}
503
511void vec2_limit_length_ptr(vector2 *res, const vector2 *v, const vm_float_t max_len)
512{
513 const vm_float_t len = vec2_length(*v);
514 if (len <= max_len || len == 0.0f) {
515 *res = *v;
516 return;
517 }
518 const vm_float_t scale = max_len / len;
519 res->x = v->x * scale;
520 res->y = v->y * scale;
521}
522
530void vec2_to_vec3_ptr(vector3 *res, const vector2 *v, const vm_float_t z)
531{
532 res->x = v->x;
533 res->y = v->y;
534 res->z = z;
535}
vm_float_t y
Definition vecmat.h:128
vm_float_t x
Definition vecmat.h:127
vm_float_t z
Definition vecmat.h:139
vm_float_t y
Definition vecmat.h:138
vm_float_t x
Definition vecmat.h:137
#define VECMAT_FMAX(a, b)
Definition vecmat.h:423
#define VECMAT_FLOOR(x)
Definition vecmat.h:424
#define VECMAT_FMIN(a, b)
Definition vecmat.h:422
#define VECMAT_ROUND(x)
Definition vecmat.h:426
#define VECMAT_COPYSIGN(x, y)
Definition vecmat.h:428
#define VECMAT_CEIL(x)
Definition vecmat.h:425
#define VECMAT_SIN(x)
Definition vecmat.h:415
double vm_float_t
Definition vecmat.h:79
#define VECMAT_FABS(x)
Definition vecmat.h:413
#define VECMAT_SQRT(x)
Definition vecmat.h:414
vm_float_t vec2_dot(vector2 a, vector2 b)
Computes the dot product of two vectors.
Definition vector2.c:439
vm_float_t vec2_length(vector2 v)
Computes the length (magnitude) of the vector.
Definition vector2.c:450
#define VECMAT_COS(x)
Definition vecmat.h:416
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_move_toward_ptr(vector2 *res, const vector2 *current, const vector2 *target, const vm_float_t max_delta)
Moves current toward target by at most max_delta.
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_normalize_ptr(vector2 *res, const vector2 *v)
Normalizes vector v to unit length, storing the result in res.
void vec2_sub_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
Subtracts a scalar from each component.
void vec2_clamp_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t min, const vm_float_t max)
Clamps each component to the scalar range [min, max].
void vec2_add_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
Adds a scalar to each component.
void vec2_lerp_ptr(vector2 *res, const vector2 *a, const vector2 *b, const vm_float_t t)
Linearly interpolates from a to b by t.
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_div_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
Divides vector v by scalar s component-wise, storing the result in res.
Definition vector2_ptr.c:53
void vec2_tangent_ptr(vector2 *res, const vector2 *v)
Computes a tangent vector perpendicular to the input (90 degrees clockwise).
void vec2_ceil_ptr(vector2 *res, const vector2 *v)
Applies the ceil function to each component of vector v, storing the result in res.
void vec2_scale_ptr(vector2 *res, const vector2 *v, const 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_to_vec3_ptr(vector3 *res, const vector2 *v, const vm_float_t z)
Converts a vector2 to a vector3 with the given z.
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
void vec2_div_ptr(vector2 *res, const vector2 *a, const vector2 *b)
Divides two vectors component-wise.
void vec2_mul_scalar_ptr(vector2 *res, const vector2 *v, const vm_float_t s)
Multiplies vector v by scalar s component-wise, storing the result in res.
Definition vector2_ptr.c:40
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_rotate_around_ptr(vector2 *res, const vector2 *v, const vector2 *pivot, const vm_float_t angle)
Rotates v around pivot by angle radians.
void vec2_limit_length_ptr(vector2 *res, const vector2 *v, const vm_float_t max_len)
Clamps the vector length to max_len.
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
void vec2_refract_ptr(vector2 *res, const vector2 *incident, const vector2 *normal, const vm_float_t eta)
Computes the refraction of incident across normal with ratio eta.
void vec2_rotate_ptr(vector2 *result, const vector2 *v, const vm_float_t angle)
Rotates the input vector counterclockwise by the given angle (radians).
void vec2_fract_ptr(vector2 *res, const vector2 *v)
Returns the fractional part of each component.
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.