Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector2i_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 <stdlib.h>
6#include <vecmat.h>
7
15void vec2i_add_ptr(vector2i *res, const vector2i *a, const vector2i *b)
16{
17 res->x = a->x + b->x;
18 res->y = a->y + b->y;
19}
20
28void vec2i_sub_ptr(vector2i *res, const vector2i *a, const vector2i *b)
29{
30 res->x = a->x - b->x;
31 res->y = a->y - b->y;
32}
33
41void vec2i_mul_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
42{
43 res->x = v->x * s;
44 res->y = v->y * s;
45}
46
57void vec2i_div_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
58{
59 if (s == 0) {
60 res->x = 0;
61 res->y = 0;
62 return;
63 }
64 res->x = (vm_int_t)((vm_float_t)v->x / s);
65 res->y = (vm_int_t)((vm_float_t)v->y / s);
66}
67
76void vec2i_mul_ptr(vector2i *res, const vector2i *a, const vector2i *b)
77{
78 res->x = a->x * b->x;
79 res->y = a->y * b->y;
80}
81
88void vec2i_neg_ptr(vector2i *res, const vector2i *v)
89{
90 res->x = -v->x;
91 res->y = -v->y;
92}
93
101void vec2i_abs_ptr(vector2i *res, const vector2i *v)
102{
103 res->x = abs(v->x);
104 res->y = abs(v->y);
105}
106
117{
118 const vm_float_t len = vec2i_length(*v);
119 if (len == 0.0f) {
120 *res = *v;
121 return;
122 }
123 res->x = (vm_int_t)(v->x / len);
124 res->y = (vm_int_t)(v->y / len);
125}
126
135void vec2i_min_ptr(vector2i *res, const vector2i *a, const vector2i *b)
136{
137 res->x = a->x < b->x ? a->x : b->x;
138 res->y = a->y < b->y ? a->y : b->y;
139}
140
149void vec2i_max_ptr(vector2i *res, const vector2i *a, const vector2i *b)
150{
151 res->x = a->x > b->x ? a->x : b->x;
152 res->y = a->y > b->y ? a->y : b->y;
153}
154
162void vec2i_sign_ptr(vector2i *res, const vector2i *v)
163{
164 res->x = v->x > 0 ? 1 : v->x < 0 ? -1 : 0;
165 res->y = v->y > 0 ? 1 : v->y < 0 ? -1 : 0;
166}
167
176{
177 res->x = -v->y;
178 res->y = v->x;
179}
180
189void vec2i_cross_ptr(vector2i *res, const vector2i *a, const vector2i *b)
190{
191 res->x = a->x * b->y - a->y * b->x;
192 res->y = 0;
193}
194
195// Floor division: toward -inf; returns 0 if b == 0.
196static vm_int_t vm_div_floor(const vm_int_t a, const vm_int_t b)
197{
198 if (b == 0) {
199 return 0;
200 }
201 const vm_int_t q = a / b;
202 const vm_int_t r = a % b;
203 if (r != 0 && ((a < 0) != (b < 0))) {
204 return q - 1;
205 }
206 return q;
207}
208
209// Floor modulo: result in [0, b) for b > 0 (or (b, 0] for b < 0); 0 if b == 0.
210static vm_int_t vm_mod_floor(const vm_int_t a, const vm_int_t b)
211{
212 if (b == 0) {
213 return 0;
214 }
215 const vm_int_t r = a % b;
216 if (r != 0 && ((a < 0) != (b < 0))) {
217 return r + b;
218 }
219 return r;
220}
221
230void vec2i_lerp_ptr(vector2i *res, const vector2i *a, const vector2i *b, const vm_float_t t)
231{
232 res->x = (vm_int_t)((1.0f - t) * (vm_float_t)a->x + t * (vm_float_t)b->x);
233 res->y = (vm_int_t)((1.0f - t) * (vm_float_t)a->y + t * (vm_float_t)b->y);
234}
235
244void vec2i_clamp_ptr(vector2i *res, const vector2i *v, const vector2i *min, const vector2i *max)
245{
246 res->x = v->x < min->x ? min->x : (v->x > max->x ? max->x : v->x);
247 res->y = v->y < min->y ? min->y : (v->y > max->y ? max->y : v->y);
248}
249
257void vec2i_div_ptr(vector2i *res, const vector2i *a, const vector2i *b)
258{
259 res->x = (b->x == 0) ? 0 : a->x / b->x;
260 res->y = (b->y == 0) ? 0 : a->y / b->y;
261}
262
270void vec2i_add_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
271{
272 res->x = v->x + s;
273 res->y = v->y + s;
274}
275
283void vec2i_sub_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
284{
285 res->x = v->x - s;
286 res->y = v->y - s;
287}
288
296void vec2i_mod_ptr(vector2i *res, const vector2i *a, const vector2i *b)
297{
298 res->x = vm_mod_floor(a->x, b->x);
299 res->y = vm_mod_floor(a->y, b->y);
300}
301
309void vec2i_div_floor_ptr(vector2i *res, const vector2i *a, const vector2i *b)
310{
311 res->x = vm_div_floor(a->x, b->x);
312 res->y = vm_div_floor(a->y, b->y);
313}
314
322void vec2i_wrap_ptr(vector2i *res, const vector2i *v, const vector2i *period)
323{
324 res->x = vm_mod_floor(v->x, period->x);
325 res->y = vm_mod_floor(v->y, period->y);
326}
327
335void vec2i_to_vec3i_ptr(vector3i *res, const vector2i *v, const vm_int_t z)
336{
337 res->x = v->x;
338 res->y = v->y;
339 res->z = z;
340}
341
349{
350 const vector2 f = {.x = (vm_float_t)v->x, .y = (vm_float_t)v->y};
351 vec2_normalize_ptr(res, &f);
352}
vm_int_t y
Definition vecmat.h:165
vm_int_t x
Definition vecmat.h:164
vm_int_t x
Definition vecmat.h:174
vm_int_t y
Definition vecmat.h:175
vm_int_t z
Definition vecmat.h:176
void vec2_normalize_ptr(vector2 *res, const vector2 *v)
Normalizes vector v to unit length, storing the result in res.
double vm_float_t
Definition vecmat.h:79
int8_t vm_int_t
Definition vecmat.h:103
vm_float_t vec2i_length(vector2i v)
Computes the length (magnitude) of the vector.
Definition vector2i.c:293
void vec2i_perpendicular_ptr(vector2i *res, const vector2i *v)
Computes the perpendicular vector to v (90 degrees counterclockwise rotation), storing the result in ...
void vec2i_div_floor_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Component-wise floored division of a by b.
void vec2i_sub_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Subtracts vector b from vector a component-wise, storing the result in res.
void vec2i_div_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
Divides vector v by scalar s component-wise (float division, truncated to int), storing the result in...
void vec2i_div_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Divides two vectors component-wise.
void vec2i_min_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Computes the component-wise minimum of vectors a and b, storing the result in res.
void vec2i_add_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
Adds a scalar to each component.
void vec2i_neg_ptr(vector2i *res, const vector2i *v)
Negates the components of vector v, storing the result in res.
void vec2i_sub_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
Subtracts a scalar from each component.
void vec2i_lerp_ptr(vector2i *res, const vector2i *a, const vector2i *b, const vm_float_t t)
Linearly interpolates from a to b by t.
void vec2i_mul_scalar_ptr(vector2i *res, const vector2i *v, const vm_int_t s)
Multiplies vector v by scalar s component-wise, storing the result in res.
void vec2i_mul_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Multiplies vectors a and b component-wise (Hadamard product), storing the result in res.
void vec2i_abs_ptr(vector2i *res, const vector2i *v)
Computes the absolute values of the components of vector v (using int abs), storing the result in res...
static vm_int_t vm_div_floor(const vm_int_t a, const vm_int_t b)
void vec2i_mod_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Component-wise floor modulo of a by b.
static vm_int_t vm_mod_floor(const vm_int_t a, const vm_int_t b)
void vec2i_normalize_ptr(vector2i *res, const vector2i *v)
Normalizes vector v to approximate unit length (float length computation, truncated to int),...
void vec2i_clamp_ptr(vector2i *res, const vector2i *v, const vector2i *min, const vector2i *max)
Clamps each component between min and max.
void vec2i_add_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Adds vectors a and b component-wise, storing the result in res.
void vec2i_normalize_to_vec2_ptr(vector2 *res, const vector2i *v)
Converts to a unit-length vector2.
void vec2i_wrap_ptr(vector2i *res, const vector2i *v, const vector2i *period)
Wraps each component of v into [0, period).
void vec2i_max_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Computes the component-wise maximum of vectors a and b, storing the result in res.
void vec2i_cross_ptr(vector2i *res, const vector2i *a, const vector2i *b)
Computes the 2D cross-product of a and b, storing the scalar value in res->x and 0 in res->y.
void vec2i_to_vec3i_ptr(vector3i *res, const vector2i *v, const vm_int_t z)
Converts a vector4i to a vector3i by dropping w.
void vec2i_sign_ptr(vector2i *res, const vector2i *v)
Sets each component of res to the sign of the corresponding component in v (+1, -1,...