Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1
16#ifndef BJ_VEC_H
17#define BJ_VEC_H
18
19#include <banjo/api.h>
20#include <banjo/math.h>
21
30typedef struct bj_vec2_t bj_vec2;
31
32#define BJ_VEC2_ZERO ((bj_vec2){BJ_FZERO, BJ_FZERO})
33
43typedef struct bj_vec3_t bj_vec3;
44
45#define BJ_VEC3_ZERO ((bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})
46
57typedef struct bj_vec4_t bj_vec4;
58
59#define BJ_VEC4_ZERO ((bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})
60
62 bj_vec2 a,
63 bj_real(*f)(bj_real)
64) {
65 return (bj_vec2){ .x = f(a.x), .y = f(a.y), };
66}
67
77 return (bj_vec2){ .x = lhs.x + rhs.x, .y = lhs.y + rhs.y, };
78}
79
88 bj_vec2 lhs,
89 bj_vec2 rhs,
90 bj_real s
91) {
92 return (bj_vec2){ .x = lhs.x + rhs.x * s, .y = lhs.y + rhs.y * s, };
93}
94
102 const bj_vec2 lhs,
103 const bj_vec2 rhs
104) {
105 return (bj_vec2){ .x = lhs.x - rhs.x, .y = lhs.y - rhs.y, };
106}
107
115 return (bj_vec2){ .x = v.x * s, .y = v.y * s, };
116}
117
125 const bj_vec2 v,
126 const bj_vec2 s
127) {
128 return (bj_vec2){ .x = v.x * s.x, .y = v.y * s.y, };
129}
130
138 return a.x * b.x + a.y * b.y;
139}
140
146 return a.x*b.y - a.y*b.x;
147}
148
155 return bj_sqrt(v.x * v.x + v.y * v.y);
156}
157
166 const bj_real l = bj_vec2_len(v);
167 if (bj_real_is_zero(l)){
168 return (bj_vec2){BJ_FZERO,BJ_FZERO};
169 }
170 return bj_vec2_scale(v, L / l);
171}
172
180 const bj_real dx = a.x - b.x;
181 const bj_real dy = a.y - b.y;
182 return dx * dx + dy * dy;
183}
184
192 return bj_sqrt(bj_vec2_distance_sq(a, b));
193}
194
209 const bj_real l2 = v.x * v.x + v.y * v.y;
210 if (bj_real_is_zero(l2)) {
211 return (bj_vec2){ BJ_FZERO, BJ_FZERO };
212 }
213 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
214 return (bj_vec2){ v.x * inv, v.y * inv };
215}
216
231 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y);
232 return (bj_vec2){ v.x * inv, v.y * inv };
233}
234
235
243 return (bj_vec2){a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, };
244}
245
253 return (bj_vec2){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, };
254}
255
257 bj_vec3 a,
258 bj_real(*f)(bj_real)
259) {
260 return (bj_vec3){ .x = f(a.x), .y = f(a.y), .z = f(a.z), };
261}
262
270 return (bj_vec3){
271 .x = lhs.x + rhs.x,
272 .y = lhs.y + rhs.y,
273 .z = lhs.z + rhs.z,
274 };
275}
276
285 bj_vec3 lhs,
286 bj_vec3 rhs,
287 bj_real s
288) {
289 return (bj_vec3){
290 .x = lhs.x + rhs.x * s,
291 .y = lhs.y + rhs.y * s,
292 .z = lhs.z + rhs.z * s,
293 };
294}
295
296
304 bj_vec3 lhs,
305 bj_vec3 rhs
306) {
307 return (bj_vec3){
308 .x = lhs.x - rhs.x,
309 .y = lhs.y - rhs.y,
310 .z = lhs.z - rhs.z,
311 };
312}
313
321 return (bj_vec3){
322 .x = v.x * s,
323 .y = v.y * s,
324 .z = v.z * s,
325 };
326}
327
335 return a.x * b.x + a.y * b.y + a.z * b.z;
336}
337
344 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
345}
346
355 const bj_real l = bj_vec3_len(v);
356 if (bj_real_is_zero(l)) {
358 }
359 return bj_vec3_scale(v, L / l);
360}
361
369 const bj_real dx = a.x - b.x;
370 const bj_real dy = a.y - b.y;
371 const bj_real dz = a.z - b.z;
372 return dx * dx + dy * dy + dz * dz;
373}
374
384
398 const bj_real l2 = v.x * v.x + v.y * v.y + v.z * v.z;
399 if (bj_real_is_zero(l2)) {
400 return (bj_vec3){ BJ_FZERO, BJ_FZERO, BJ_FZERO };
401 }
402 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
403 return (bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
404}
405
419 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
420 return (bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
421}
422
430 return (bj_vec3){
431 a.x < b.x ? a.x : b.x,
432 a.y < b.y ? a.y : b.y,
433 a.z < b.z ? a.z : b.z,
434 };
435}
436
444 return (bj_vec3){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z,};
445}
446
454{
455 return (bj_vec3) {
456 .x = l.y * r.z - l.z * r.y,
457 .y = l.z * r.x - l.x * r.z,
458 .z = l.x * r.y - l.y * r.x,
459 };
460}
461
470{
471 const bj_real p = BJ_F(2.) * bj_vec3_dot(v, n);
472 return (bj_vec3) {
473 .x = v.x - p * n.x,
474 .y = v.y - p * n.y,
475 .z = v.z - p * n.z,
476 };
477}
478
480 bj_vec4 a,
481 bj_real(*f)(bj_real)
482) {
483 return (bj_vec4){ .x = f(a.x), .y = f(a.y), .z = f(a.z), .w = f(a.w), };
484}
485
493 return (bj_vec4){
494 .x = lhs.x + rhs.x,
495 .y = lhs.y + rhs.y,
496 .z = lhs.z + rhs.z,
497 .w = lhs.w + rhs.w,
498 };
499}
500
509 bj_vec4 lhs,
510 bj_vec4 rhs,
511 bj_real s
512) {
513 return (bj_vec4){
514 .x = lhs.x + rhs.x * s,
515 .y = lhs.y + rhs.y * s,
516 .z = lhs.z + rhs.z * s,
517 .w = lhs.w + rhs.w * s,
518 };
519}
520
528 bj_vec4 lhs,
529 bj_vec4 rhs
530) {
531 return (bj_vec4){
532 .x = lhs.x - rhs.x,
533 .y = lhs.y - rhs.y,
534 .z = lhs.z - rhs.z,
535 .w = lhs.w - rhs.w,
536 };
537}
538
546 return (bj_vec4){
547 .x = v.x * s,
548 .y = v.y * s,
549 .z = v.z * s,
550 .w = v.w * s,
551 };
552}
553
561 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
562}
563
570 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
571}
572
586 const bj_real len2 = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
587 if (bj_real_is_zero(len2)) {
589 }
590 const bj_real inv = BJ_F(1.0) / bj_sqrt(len2);
591 return (bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
592}
593
607 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
608 return (bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
609}
610
618 return (bj_vec4){
619 a.x < b.x ? a.x : b.x,
620 a.y < b.y ? a.y : b.y,
621 a.z < b.z ? a.z : b.z,
622 a.w < b.w ? a.w : b.w,
623 };
624}
625
633 return (bj_vec4){
634 a.x > b.x ? a.x : b.x,
635 a.y > b.y ? a.y : b.y,
636 a.z > b.z ? a.z : b.z,
637 a.w > b.w ? a.w : b.w,
638 };
639}
640
649 return (bj_vec4){
650 l.y*r.z - l.z*r.y,
651 l.z*r.x - l.x*r.z,
652 l.x*r.y - l.y*r.x,
654 };
655}
656
665{
666 bj_real p = BJ_F(2.0) * bj_vec4_dot(v, n);
667 return (bj_vec4) {
668 .x = v.x - p * n.x,
669 .y = v.y - p * n.y,
670 .z = v.z - p * n.z,
671 .w = v.w - p * n.w,
672 };
673}
674
675
676#endif
677
679
General-purpose definitions for Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:217
bj_real y
Definition vec.h:40
bj_real z
Definition vec.h:54
bj_real x
Definition vec.h:52
bj_real y
Definition vec.h:28
bj_real y
Definition vec.h:53
bj_real x
Definition vec.h:39
bj_real z
Definition vec.h:41
bj_real x
Definition vec.h:27
bj_real w
Definition vec.h:55
struct bj_vec3_t bj_vec3
Definition vec.h:43
static bj_vec3 bj_vec3_max(bj_vec3 a, bj_vec3 b)
Component-wise maximum of two 3D vectors.
Definition vec.h:443
static bj_vec4 bj_vec4_normalize_unsafe(bj_vec4 v)
Normalize a 4D vector to unit length (unsafe).
Definition vec.h:606
static bj_vec4 bj_vec4_min(bj_vec4 a, bj_vec4 b)
Component-wise minimum of two 4D vectors.
Definition vec.h:617
static bj_vec3 bj_vec3_sub(bj_vec3 lhs, bj_vec3 rhs)
Component-wise subtraction of two 3D vectors: res = lhs - rhs.
Definition vec.h:303
static bj_vec3 bj_vec3_min(bj_vec3 a, bj_vec3 b)
Component-wise minimum of two 3D vectors.
Definition vec.h:429
static bj_real bj_vec2_len(bj_vec2 v)
Euclidean length (L2 norm) of a 2D vector.
Definition vec.h:154
static bj_vec2 bj_vec2_normalize_unsafe(bj_vec2 v)
Normalize a 2D vector to unit length (unsafe).
Definition vec.h:230
static bj_real bj_vec2_distance_sq(bj_vec2 a, bj_vec2 b)
Squared Euclidean distance between two 2D vectors.
Definition vec.h:179
static bj_vec2 bj_vec2_min(bj_vec2 a, bj_vec2 b)
Component-wise minimum of two 2D vectors.
Definition vec.h:242
static bj_real bj_vec3_len(bj_vec3 v)
Euclidean length (L2 norm) of a 3D vector.
Definition vec.h:343
static bj_real bj_vec3_distance(bj_vec3 a, bj_vec3 b)
Euclidean distance between two 3D vectors.
Definition vec.h:381
static bj_vec3 bj_vec3_reflect(bj_vec3 v, bj_vec3 n)
Reflect a vector about a normal: res = v - 2*dot(v, n)*n.
Definition vec.h:469
static bj_vec2 bj_vec2_max(bj_vec2 a, bj_vec2 b)
Component-wise maximum of two 2D vectors.
Definition vec.h:252
static bj_vec3 bj_vec3_normalize_unsafe(bj_vec3 v)
Normalize a 3D vector to unit length (unsafe).
Definition vec.h:418
static bj_vec2 bj_vec2_scale_to_len(bj_vec2 v, bj_real L)
Scale a 2D vector to a given length.
Definition vec.h:165
struct bj_vec2_t bj_vec2
Definition vec.h:30
static bj_real bj_vec2_perp_dot(bj_vec2 a, bj_vec2 b)
2D "cross product" (perp dot): returns scalar a.x*b.y - a.y*b.x.
Definition vec.h:145
struct bj_vec4_t bj_vec4
Definition vec.h:57
static bj_vec2 bj_vec2_add(bj_vec2 lhs, bj_vec2 rhs)
Component-wise addition of two 2D vectors: res = lhs + rhs.
Definition vec.h:76
static bj_real bj_vec4_dot(bj_vec4 a, bj_vec4 b)
Dot product of two 4D vectors.
Definition vec.h:560
static bj_vec2 bj_vec2_add_scaled(bj_vec2 lhs, bj_vec2 rhs, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:87
static bj_vec4 bj_vec4_add(bj_vec4 lhs, bj_vec4 rhs)
Component-wise addition of two 4D vectors: res = lhs + rhs.
Definition vec.h:492
static bj_vec4 bj_vec4_normalize(bj_vec4 v)
Normalize a 4D vector to unit length (safe).
Definition vec.h:585
static bj_vec2 bj_vec2_map(bj_vec2 a, bj_real(*f)(bj_real))
Definition vec.h:61
static bj_vec3 bj_vec3_normalize(bj_vec3 v)
Normalize a 3D vector to unit length (safe).
Definition vec.h:397
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:64
static bj_vec2 bj_vec2_normalize(bj_vec2 v)
Normalize a 2D vector to unit length (safe).
Definition vec.h:208
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:441
static bj_vec4 bj_vec4_cross_xyz(bj_vec4 l, bj_vec4 r)
Cross product using xyz components; w is set to 1.
Definition vec.h:648
static bj_real bj_vec2_dot(bj_vec2 a, bj_vec2 b)
Dot product of two 2D vectors.
Definition vec.h:137
static bj_vec3 bj_vec3_cross(bj_vec3 l, bj_vec3 r)
3D cross product: res = l × r (right-hand rule).
Definition vec.h:453
static bj_vec2 bj_vec2_sub(const bj_vec2 lhs, const bj_vec2 rhs)
Component-wise subtraction of two 2D vectors: res = lhs - rhs.
Definition vec.h:101
static bj_real bj_vec2_distance(const bj_vec2 a, const bj_vec2 b)
Euclidean distance between two 2D vectors.
Definition vec.h:191
static bj_vec4 bj_vec4_add_scaled(bj_vec4 lhs, bj_vec4 rhs, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:508
static bj_vec4 bj_vec4_map(bj_vec4 a, bj_real(*f)(bj_real))
Definition vec.h:479
static bj_vec3 bj_vec3_map(bj_vec3 a, bj_real(*f)(bj_real))
Definition vec.h:256
static bj_vec3 bj_vec3_add_scaled(bj_vec3 lhs, bj_vec3 rhs, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:284
static bj_vec4 bj_vec4_max(bj_vec4 a, bj_vec4 b)
Component-wise maximum of two 4D vectors.
Definition vec.h:632
static bj_real bj_vec3_dot(bj_vec3 a, bj_vec3 b)
Dot product of two 3D vectors.
Definition vec.h:334
static bj_vec3 bj_vec3_scale_to_len(bj_vec3 v, bj_real L)
Scale a 3D vector to a given length.
Definition vec.h:354
static bj_real bj_vec3_distance_sq(bj_vec3 a, bj_vec3 b)
Squared Euclidean distance between two 3D vectors.
Definition vec.h:368
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
static bj_vec2 bj_vec2_mul_comp(const bj_vec2 v, const bj_vec2 s)
Per-component scaling: res[i] = v[i] * s[i].
Definition vec.h:124
static bj_real bj_vec4_len(bj_vec4 v)
Euclidean length (L2 norm) of a 4D vector.
Definition vec.h:569
static bj_vec4 bj_vec4_scale(bj_vec4 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:545
#define bj_sqrt
Square root.
Definition math.h:222
float bj_real
Selected real type for float configuration.
Definition math.h:51
static bj_vec4 bj_vec4_reflect(bj_vec4 v, bj_vec4 n)
Reflect a vector about a normal: res = v - 2*dot(v, n)*n.
Definition vec.h:664
static bj_vec3 bj_vec3_add(bj_vec3 lhs, bj_vec3 rhs)
Component-wise addition of two 3D vectors: res = lhs + rhs.
Definition vec.h:269
static bj_vec3 bj_vec3_scale(bj_vec3 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:320
static bj_vec2 bj_vec2_scale(bj_vec2 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:114
static bj_vec4 bj_vec4_sub(bj_vec4 lhs, bj_vec4 rhs)
Component-wise subtraction of two 4D vectors: res = lhs - rhs.
Definition vec.h:527
bj_vec2: 2D vector of bj_real values.
Definition vec.h:26
bj_vec3: 3D vector of bj_real values.
Definition vec.h:38
bj_vec4: 4D vector of bj_real values.
Definition vec.h:51
C99 math shim with bj_real precision type and scalar utilities.