Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
Math
Collaboration diagram for Math:

Topics

 Geometry 2D

Data Structures

struct  bj_mat3x3
struct  bj_mat3x2
struct  bj_mat4x4
struct  bj_mat4x3
struct  bj_rect
struct  bj_vec2
struct  bj_vec3
struct  bj_vec4

Macros

#define BJ_M3(c, r)
#define BJ_M32(c, r)
#define BJ_M4(c, r)
#define BJ_M43(c, r)
#define BJ_VEC2_ZERO   ((struct bj_vec2){BJ_FZERO, BJ_FZERO})
#define BJ_VEC3_ZERO   ((struct bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})
#define BJ_VEC4_ZERO   ((struct bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})

Functions

static void bj_mat3_set_identity (struct bj_mat3x3 *restrict M)
static void bj_mat3_copy (struct bj_mat3x3 *restrict dst, const struct bj_mat3x3 *restrict src)
static struct bj_vec3 bj_mat3_row (const struct bj_mat3x3 *restrict M, int r)
static struct bj_vec3 bj_mat3_col (const struct bj_mat3x3 *restrict M, int c)
static void bj_mat3_transpose (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
static void bj_mat3_add (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
static void bj_mat3_sub (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
static void bj_mat3_mul_scalar (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, bj_real k)
static void bj_mat3_mul (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A, const struct bj_mat3x3 *restrict B)
static struct bj_vec3 bj_mat3_transform_vec3 (const struct bj_mat3x3 *restrict M, struct bj_vec3 v)
static struct bj_vec2 bj_mat3_transform_point (const struct bj_mat3x3 *restrict M, struct bj_vec2 p)
static void bj_mat3_set_translation (struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3_translate (struct bj_mat3x3 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3_set_scaling_xy (struct bj_mat3x3 *restrict M, bj_real sx, bj_real sy)
static void bj_mat3_set_shear_xy (struct bj_mat3x3 *restrict M, bj_real shx, bj_real shy)
static void bj_mat3_set_rotation_z (struct bj_mat3x3 *restrict M, bj_real angle)
static bj_real bj_mat3_determinant (const struct bj_mat3x3 *restrict A)
static bj_bool bj_mat3_invert (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
static void bj_mat3_invert_unsafe (struct bj_mat3x3 *restrict out, const struct bj_mat3x3 *restrict A)
static void bj_mat3_set_ortho (struct bj_mat3x3 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t)
static void bj_mat3_set_viewport (struct bj_mat3x3 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
static void bj_mat3x2_set_identity (struct bj_mat3x2 *restrict M)
static void bj_mat3x2_set_translation (struct bj_mat3x2 *restrict M, bj_real tx, bj_real ty)
static void bj_mat3x2_set_scaling_xy (struct bj_mat3x2 *restrict M, bj_real sx, bj_real sy)
static void bj_mat3x2_set_rotation_z (struct bj_mat3x2 *restrict M, bj_real angle)
static void bj_mat3x2_mul (struct bj_mat3x2 *restrict out, const struct bj_mat3x2 *restrict A, const struct bj_mat3x2 *restrict B)
static struct bj_vec2 bj_mat3x2_transform_point (const struct bj_mat3x2 *restrict M, struct bj_vec2 p)
static struct bj_vec2 bj_mat3x2_transform_dir (const struct bj_mat3x2 *restrict M, struct bj_vec2 v)
static void bj_mat3_from_mat3x2 (struct bj_mat3x3 *restrict M, const struct bj_mat3x2 *restrict A)
static void bj_mat3x2_from_mat3 (struct bj_mat3x2 *restrict M, const struct bj_mat3x3 *restrict A)
static void bj_mat4_set_identity (struct bj_mat4x4 *restrict M)
static void bj_mat4_copy (struct bj_mat4x4 *restrict dst, const struct bj_mat4x4 *restrict src)
static struct bj_vec4 bj_mat4_row (const struct bj_mat4x4 *restrict M, int r)
static struct bj_vec4 bj_mat4_col (const struct bj_mat4x4 *restrict M, int c)
static void bj_mat4_transpose (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
static void bj_mat4_add (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
static void bj_mat4_sub (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
static void bj_mat4_mul_scalar (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real k)
static void bj_mat4_scale_axes (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, bj_real sx, bj_real sy, bj_real sz)
static void bj_mat4_mul (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A, const struct bj_mat4x4 *restrict B)
static struct bj_vec4 bj_mat4_transform_vec4 (const struct bj_mat4x4 *restrict M, struct bj_vec4 v)
static void bj_mat4_set_translation (struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_translate (struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real z)
static void bj_mat4_set_outer_product (struct bj_mat4x4 *restrict out, struct bj_vec3 a, struct bj_vec3 b)
static void bj_mat4_rotate_axis_angle (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, struct bj_vec3 axis, bj_real angle)
static void bj_mat4_rotate_x (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
static void bj_mat4_rotate_y (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
static void bj_mat4_rotate_z (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M, bj_real a)
static void bj_mat4_rotate_arcball (struct bj_mat4x4 *restrict R, const struct bj_mat4x4 *restrict M, struct bj_vec2 a, struct bj_vec2 b, bj_real s)
static bj_bool bj_mat4_invert (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
static void bj_mat4_invert_unsafe (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict M)
static void bj_mat4_orthonormalize (struct bj_mat4x4 *restrict out, const struct bj_mat4x4 *restrict A)
static void bj_mat4_set_frustum (struct bj_mat4x4 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t, bj_real n, bj_real f)
static void bj_mat4_set_ortho (struct bj_mat4x4 *restrict M, bj_real l, bj_real r, bj_real b, bj_real t, bj_real n, bj_real f)
static void bj_mat4_set_perspective (struct bj_mat4x4 *restrict M, bj_real y_fov, bj_real aspect, bj_real n, bj_real f)
static void bj_mat4_set_viewport (struct bj_mat4x4 *restrict M, bj_real x, bj_real y, bj_real w, bj_real h)
static void bj_mat4_set_lookat (struct bj_mat4x4 *restrict M, struct bj_vec3 eye, struct bj_vec3 center, struct bj_vec3 up)
static void bj_mat4x3_set_identity (struct bj_mat4x3 *restrict M)
static void bj_mat4x3_set_translation (struct bj_mat4x3 *restrict M, bj_real tx, bj_real ty, bj_real tz)
static void bj_mat4x3_set_scaling_xyz (struct bj_mat4x3 *restrict M, bj_real sx, bj_real sy, bj_real sz)
static void bj_mat4x3_set_rotation_x (struct bj_mat4x3 *restrict M, bj_real a)
static void bj_mat4x3_set_rotation_y (struct bj_mat4x3 *restrict M, bj_real a)
static void bj_mat4x3_set_rotation_z (struct bj_mat4x3 *restrict M, bj_real a)
static void bj_mat4x3_mul (struct bj_mat4x3 *restrict out, const struct bj_mat4x3 *restrict A, const struct bj_mat4x3 *restrict B)
static struct bj_vec3 bj_mat4x3_transform_point (const struct bj_mat4x3 *restrict M, struct bj_vec3 p)
static struct bj_vec3 bj_mat4x3_transform_dir (const struct bj_mat4x3 *restrict M, struct bj_vec3 v)
static void bj_mat4_from_mat4x3 (struct bj_mat4x4 *restrict M, const struct bj_mat4x3 *restrict A)
static void bj_mat4x3_from_mat4 (struct bj_mat4x3 *restrict M, const struct bj_mat4x4 *restrict A)
static struct bj_vec4 bj_quat_identity (void)
static bj_real bj_quat_dot (struct bj_vec4 a, struct bj_vec4 b)
static bj_real bj_quat_norm2 (struct bj_vec4 q)
static bj_real bj_quat_norm (struct bj_vec4 q)
static struct bj_vec4 bj_quat_normalize (struct bj_vec4 q)
static struct bj_vec4 bj_quat_conjugate (struct bj_vec4 q)
static struct bj_vec4 bj_quat_inverse (struct bj_vec4 q)
static struct bj_vec4 bj_quat_mul (struct bj_vec4 p, struct bj_vec4 q)
static struct bj_vec4 bj_quat_slerp (struct bj_vec4 a, struct bj_vec4 b, bj_real t)
static struct bj_vec4 bj_quat_from_axis_angle (struct bj_vec3 axis, bj_real angle_rad)
static struct bj_vec3 bj_quat_rotate_vec3 (struct bj_vec4 q, struct bj_vec3 v)
static struct bj_vec4 bj_quat_rotate_vec4 (struct bj_vec4 q, struct bj_vec4 v)
static void bj_quat_to_mat4 (struct bj_mat4x4 *restrict M, struct bj_vec4 q)
static struct bj_vec4 bj_quat_from_mat4 (const struct bj_mat4x4 *restrict M)
bj_bool bj_rect_intersection (const struct bj_rect *rect_a, const struct bj_rect *rect_b, struct bj_rect *result)
void bj_rect_union (const struct bj_rect *rect_a, const struct bj_rect *rect_b, struct bj_rect *result)
static struct bj_vec2 bj_vec2_map (struct bj_vec2 a, bj_real(*f)(bj_real))
static struct bj_vec2 bj_vec2_add (struct bj_vec2 lhs, struct bj_vec2 rhs)
static struct bj_vec2 bj_vec2_add_scaled (struct bj_vec2 lhs, struct bj_vec2 rhs, bj_real s)
static struct bj_vec2 bj_vec2_sub (const struct bj_vec2 lhs, const struct bj_vec2 rhs)
static struct bj_vec2 bj_vec2_scale (struct bj_vec2 v, bj_real s)
static struct bj_vec2 bj_vec2_mul_comp (const struct bj_vec2 v, const struct bj_vec2 s)
static bj_real bj_vec2_dot (struct bj_vec2 a, struct bj_vec2 b)
static bj_real bj_vec2_perp_dot (struct bj_vec2 a, struct bj_vec2 b)
static bj_real bj_vec2_len (struct bj_vec2 v)
static struct bj_vec2 bj_vec2_scale_to_len (struct bj_vec2 v, bj_real L)
static bj_real bj_vec2_distance_sq (struct bj_vec2 a, struct bj_vec2 b)
static bj_real bj_vec2_distance (const struct bj_vec2 a, const struct bj_vec2 b)
static struct bj_vec2 bj_vec2_normalize (struct bj_vec2 v)
static struct bj_vec2 bj_vec2_normalize_unsafe (struct bj_vec2 v)
static struct bj_vec2 bj_vec2_min (struct bj_vec2 a, struct bj_vec2 b)
static struct bj_vec2 bj_vec2_max (struct bj_vec2 a, struct bj_vec2 b)
static struct bj_vec3 bj_vec3_map (struct bj_vec3 a, bj_real(*f)(bj_real))
static struct bj_vec3 bj_vec3_add (struct bj_vec3 lhs, struct bj_vec3 rhs)
static struct bj_vec3 bj_vec3_add_scaled (struct bj_vec3 lhs, struct bj_vec3 rhs, bj_real s)
static struct bj_vec3 bj_vec3_sub (struct bj_vec3 lhs, struct bj_vec3 rhs)
static struct bj_vec3 bj_vec3_scale (struct bj_vec3 v, bj_real s)
static bj_real bj_vec3_dot (struct bj_vec3 a, struct bj_vec3 b)
static bj_real bj_vec3_len (struct bj_vec3 v)
static struct bj_vec3 bj_vec3_scale_to_len (struct bj_vec3 v, bj_real L)
static bj_real bj_vec3_distance_sq (struct bj_vec3 a, struct bj_vec3 b)
static bj_real bj_vec3_distance (struct bj_vec3 a, struct bj_vec3 b)
static struct bj_vec3 bj_vec3_normalize (struct bj_vec3 v)
static struct bj_vec3 bj_vec3_normalize_unsafe (struct bj_vec3 v)
static struct bj_vec3 bj_vec3_min (struct bj_vec3 a, struct bj_vec3 b)
static struct bj_vec3 bj_vec3_max (struct bj_vec3 a, struct bj_vec3 b)
static struct bj_vec3 bj_vec3_cross (struct bj_vec3 l, struct bj_vec3 r)
static struct bj_vec3 bj_vec3_reflect (struct bj_vec3 v, struct bj_vec3 n)
static struct bj_vec4 bj_vec4_map (struct bj_vec4 a, bj_real(*f)(bj_real))
static struct bj_vec4 bj_vec4_add (struct bj_vec4 lhs, struct bj_vec4 rhs)
static struct bj_vec4 bj_vec4_add_scaled (struct bj_vec4 lhs, struct bj_vec4 rhs, bj_real s)
static struct bj_vec4 bj_vec4_sub (struct bj_vec4 lhs, struct bj_vec4 rhs)
static struct bj_vec4 bj_vec4_scale (struct bj_vec4 v, bj_real s)
static bj_real bj_vec4_dot (struct bj_vec4 a, struct bj_vec4 b)
static bj_real bj_vec4_len (struct bj_vec4 v)
static struct bj_vec4 bj_vec4_normalize (struct bj_vec4 v)
static struct bj_vec4 bj_vec4_normalize_unsafe (struct bj_vec4 v)
static struct bj_vec4 bj_vec4_min (struct bj_vec4 a, struct bj_vec4 b)
static struct bj_vec4 bj_vec4_max (struct bj_vec4 a, struct bj_vec4 b)
static struct bj_vec4 bj_vec4_cross_xyz (struct bj_vec4 l, struct bj_vec4 r)
static struct bj_vec4 bj_vec4_reflect (struct bj_vec4 v, struct bj_vec4 n)

Real type selection and helpers

typedef float bj_real
#define BJ_F(x)
#define BJ_EPSILON   (FLT_EPSILON)
#define BJ_FI(x)
#define BJ_FZERO   (BJ_F(0.0))

Scalar utilities

Generic scalar helpers.

All arguments and results are bj_real.

static bj_real bj_clamp (bj_real x, bj_real lo, bj_real hi)
static bj_real bj_step (bj_real edge, bj_real x)
static bj_real bj_smoothstep (bj_real e0, bj_real e1, bj_real x)
static bj_real bj_fract (bj_real x)
static bj_real bj_mod (bj_real x, bj_real y)

Absolute-epsilon comparisons

Comparisons using BJ_EPSILON as an absolute tolerance.

Warning
Epsilon equality is not transitive. bj_real_eq can report a ≈ b and b ≈ c while a !≈ c whenever the chain of small differences accumulates past BJ_EPSILON. So don't use these as a drop-in replacement for == when transitivity matters (sorting, equivalence classes, hash-set keys, memoisation tables). Use them for "is the computed value close enough to the expected value?" checks, where you only ever compare against a single reference point.

The relative variants (bj_real_eq_rel and friends) have the same limitation: relative tolerance scales the threshold with magnitude but doesn't restore transitivity.

static int bj_real_eq (bj_real a, bj_real b)
static int bj_real_neq (bj_real a, bj_real b)
static int bj_real_lt (bj_real a, bj_real b)
static int bj_real_gt (bj_real a, bj_real b)
static int bj_real_lte (bj_real a, bj_real b)
static int bj_real_gte (bj_real a, bj_real b)
static int bj_real_cmp (bj_real a, bj_real b)

Relative-epsilon comparisons

Comparisons using a scale-dependent tolerance.

static bj_real bj_real_relative_scale (bj_real a, bj_real b)
static int bj_real_eq_rel (bj_real a, bj_real b)
static int bj_real_neq_rel (bj_real a, bj_real b)
static int bj_real_lt_rel (bj_real a, bj_real b)
static int bj_real_gt_rel (bj_real a, bj_real b)
static int bj_real_lte_rel (bj_real a, bj_real b)
static int bj_real_gte_rel (bj_real a, bj_real b)
static int bj_real_cmp_rel (bj_real a, bj_real b)

Zero tests and helpers

Utilities for zero checks and safe normalization.

static int bj_real_is_zero (bj_real x)
static int bj_real_is_zero_scaled (bj_real x, bj_real scale)
static bj_real bj_real_snap_zero (bj_real x)
static bj_real bj_real_snorm_safe (bj_real x, bj_real len)

Circle constants

#define BJ_PI_F   (3.14159265358979323846f)
#define BJ_TAU_F   (6.28318530717958647692f)
#define BJ_PI_D   (3.14159265358979323846264338327950288)
#define BJ_TAU_D   (6.28318530717958647692528676655900576)
#define BJ_PI_L   (3.141592653589793238462643383279502884L)
#define BJ_TAU_L   (6.283185307179586476925286766559005768L)
#define BJ_PI   (BJ_F(3.141592653589793238462643383279502884))
#define BJ_TAU   (BJ_F(6.283185307179586476925286766559005768))

Typed wrappers (float)

Thin aliases to <math.h> float functions.

#define bj_absf   fabsf
#define bj_acosf   acosf
#define bj_atan2f   atan2f
#define bj_copysignf   copysignf
#define bj_cosf   cosf
#define bj_expf   expf
#define bj_floorf   floorf
#define bj_fmodf   fmodf
#define bj_logf   logf
#define bj_maxf   fmaxf
#define bj_minf   fminf
#define bj_powf   powf
#define bj_roundf   roundf
#define bj_sinf   sinf
#define bj_sqrtf   sqrtf
#define bj_tanf   tanf

Typed wrappers (double)

Thin aliases to <math.h> double functions.

#define bj_absd   fabs
#define bj_acosd   acos
#define bj_atan2d   atan2
#define bj_copysignd   copysign
#define bj_cosd   cos
#define bj_expd   exp
#define bj_floord   floor
#define bj_fmodd   fmod
#define bj_logd   log
#define bj_maxd   fmax
#define bj_mind   fmin
#define bj_powd   pow
#define bj_roundd   round
#define bj_sind   sin
#define bj_sqrtd   sqrt
#define bj_tand   tan

Typed wrappers (long double)

Thin aliases to <math.h> long double functions.

#define bj_absl   fabsl
#define bj_acosl   acosl
#define bj_atan2l   atan2l
#define bj_copysignl   copysignl
#define bj_cosl   cosl
#define bj_expl   expl
#define bj_floorl   floorl
#define bj_fmodl   fmodl
#define bj_logl   logl
#define bj_maxl   fmaxl
#define bj_minl   fminl
#define bj_powl   powl
#define bj_roundl   roundl
#define bj_sinl   sinl
#define bj_sqrtl   sqrtl
#define bj_tanl   tanl

Precision-dispatch to match bj_real

Map generic bj_* names to the active precision.

#define bj_abs   bj_absf
#define bj_acos   bj_acosf
#define bj_atan2   bj_atan2f
#define bj_copysign   bj_copysignf
#define bj_cos   bj_cosf
#define bj_exp   bj_expf
#define bj_floor   bj_floorf
#define bj_fmod   bj_fmodf
#define bj_log   bj_logf
#define bj_max   bj_maxf
#define bj_min   bj_minf
#define bj_pow   bj_powf
#define bj_round   bj_roundf
#define bj_sin   bj_sinf
#define bj_sqrt   bj_sqrtf
#define bj_tan   bj_tanf

Detailed Description

A small math toolkit: a choose-your-precision real type, constants, the standard <math.h> functions with consistent naming, and a few extras.

Most C math is <math.h>, and the function you want depends on the floating-point type: sin for double, sinf for float, sinl for long double. Banjo wraps these so you can write code once that works regardless of the precision your build is using.

At the centre is bj_real, Banjo's "the floating-point type" that's chosen at build time. By default bj_real is float; define BJ_API_FLOAT64 or BJ_API_LONG_DOUBLE to make it double or long double respectively. Then every bj_* math function and constant follows along: bj_sin uses the right sin flavour, BJ_PI is the right precision, and so on.

What's in here:

  • A precision-agnostic real type: bj_real, plus the BJ_F literal-suffix helper and BJ_EPSILON for the smallest meaningful difference at your chosen precision.
  • Constants like BJ_PI and BJ_TAU (= 2π), provided in float, double, and long-double flavours plus a generic bj_* form that matches the selected precision.
  • Typed <math.h> wrappers (bj_sinf, bj_sind, bj_sinl, …) plus generic bj_* aliases that resolve to the active precision.
  • Small extras that aren't in standard <math.h>: bj_clamp (limit a value to a range), step / smoothstep (smooth threshold functions used in shading and animation), fract (the fractional part of a number), and safe variants of normalisation that don't blow up on zero-length inputs.
  • Floating-point comparison helpers for the "is x roughly equal to y?" question that you can't answer with == for floats.

These functions don't carry units, they treat their inputs as pure numbers. Use them as the math layer underneath domain code like Physics (which does think in units).


Data Structure Documentation

◆ bj_mat3x3

struct bj_mat3x3

3×3 column-major matrix.

Used for 2D homogeneous (projective) transforms: the 2×2 linear block plus a third column for projective terms.

Examples
physics_particle.c.

Definition at line 59 of file mat.h.

Data Fields
bj_real m[9]

◆ bj_mat3x2

struct bj_mat3x2

3×2 column-major matrix (2D affine).

A 2×2 linear block plus a translation column. No projective term: use bj_mat3x3 if you need one. Cheaper to multiply and transform with than 3×3.

Definition at line 70 of file mat.h.

Data Fields
bj_real m[6]

◆ bj_mat4x4

struct bj_mat4x4

4×4 column-major matrix.

Used for 3D homogeneous transforms and projection matrices.

Definition at line 79 of file mat.h.

Data Fields
bj_real m[16]

◆ bj_mat4x3

struct bj_mat4x3

4×3 column-major matrix (3D affine).

A 3×3 linear block plus a translation column. No projective term: use bj_mat4x4 if you need one.

Definition at line 89 of file mat.h.

Data Fields
bj_real m[12]

◆ bj_rect

struct bj_rect

Axis-aligned rectangle: a top-left corner plus a width and height.

Covers pixels [x, x+w) × [y, y+h). Position is signed so a rectangle can be partly or fully off-surface; size is unsigned.

Examples
bitmap_blit.c, bitmap_blit_colorkey.c, drawing_2d.c, drawing_text.c, interpolation.c, load_bmp.c, physics_kinematics.c, physics_particle.c, pong.c, random_distribution.c, shaders.c, sprite_animation.c, and start.c.

Definition at line 33 of file rect.h.

Data Fields
uint16_t h Height in pixels.
uint16_t w Width in pixels.
int16_t x X coordinate of the top-left corner (pixels, can be negative).
int16_t y Y coordinate of the top-left corner (pixels, can be negative).

◆ bj_vec2

struct bj_vec2

2D vector of bj_real components.

POD layout, suitable for arrays and binary interop.

Examples
physics_kinematics.c, and shaders.c.

Definition at line 51 of file vec.h.

Data Fields
bj_real x X component.
bj_real y Y component.

◆ bj_vec3

struct bj_vec3

3D vector of bj_real components.

POD layout, suitable for arrays and binary interop.

Examples
physics_particle.c, and shaders.c.

Definition at line 64 of file vec.h.

Data Fields
bj_real x X component.
bj_real y Y component.
bj_real z Z component.

◆ bj_vec4

struct bj_vec4

4D vector of bj_real components.

POD layout, suitable for arrays and binary interop. Reused as the storage for quaternions; see quat.h.

Definition at line 79 of file vec.h.

Data Fields
bj_real w W component (scalar part for quaternions).
bj_real x X component.
bj_real y Y component.
bj_real z Z component.

Macro Definition Documentation

◆ bj_abs

#define bj_abs   bj_absf

Absolute value.

Examples
shaders.c.

Definition at line 233 of file math.h.

Referenced by bj_real_eq(), bj_real_eq_rel(), bj_real_is_zero(), bj_real_is_zero_scaled(), bj_real_relative_scale(), and shader_code().

◆ bj_absd

#define bj_absd   fabs

Absolute value (double)

Definition at line 148 of file math.h.

◆ bj_absf

#define bj_absf   fabsf

Absolute value (float)

Definition at line 123 of file math.h.

◆ bj_absl

#define bj_absl   fabsl

Absolute value (long double)

Definition at line 173 of file math.h.

◆ bj_acos

#define bj_acos   bj_acosf

Arc cosine.

Definition at line 234 of file math.h.

Referenced by bj_mat4_rotate_arcball(), and bj_quat_slerp().

◆ bj_acosd

#define bj_acosd   acos

Arc cosine (double)

Definition at line 149 of file math.h.

◆ bj_acosf

#define bj_acosf   acosf

Arc cosine (float)

Definition at line 124 of file math.h.

◆ bj_acosl

#define bj_acosl   acosl

Arc cosine (long double)

Definition at line 174 of file math.h.

◆ bj_atan2

#define bj_atan2   bj_atan2f

Arc tangent.

Definition at line 235 of file math.h.

◆ bj_atan2d

#define bj_atan2d   atan2

Arc tangent (float)

Definition at line 150 of file math.h.

◆ bj_atan2f

#define bj_atan2f   atan2f

Arc tangent (float)

Definition at line 125 of file math.h.

◆ bj_atan2l

#define bj_atan2l   atan2l

Arc tangent (float)

Definition at line 175 of file math.h.

◆ bj_copysign

#define bj_copysign   bj_copysignf

Copy sign.

Definition at line 236 of file math.h.

◆ bj_copysignd

#define bj_copysignd   copysign

Copy sign (double)

Definition at line 151 of file math.h.

◆ bj_copysignf

#define bj_copysignf   copysignf

Copy sign (float)

Definition at line 126 of file math.h.

◆ bj_copysignl

#define bj_copysignl   copysignl

Copy sign (long double)

Definition at line 176 of file math.h.

◆ bj_cos

◆ bj_cosd

#define bj_cosd   cos

Cosine (double)

Definition at line 152 of file math.h.

◆ bj_cosf

#define bj_cosf   cosf

Cosine (float)

Definition at line 127 of file math.h.

◆ bj_cosl

#define bj_cosl   cosl

Cosine (long double)

Definition at line 177 of file math.h.

◆ BJ_EPSILON

#define BJ_EPSILON   (FLT_EPSILON)

◆ bj_exp

#define bj_exp   bj_expf

Exponential.

Examples
shaders.c.

Definition at line 238 of file math.h.

Referenced by shader_code().

◆ bj_expd

#define bj_expd   exp

Exponential (double)

Definition at line 153 of file math.h.

◆ bj_expf

#define bj_expf   expf

Exponential (float)

Definition at line 128 of file math.h.

◆ bj_expl

#define bj_expl   expl

Exponential (long double)

Definition at line 178 of file math.h.

◆ BJ_F

◆ BJ_FI

#define BJ_FI ( x)
Value:
BJ_F(1.0) / BJ_F(x)
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:78

Convenience reciprocal literal generator.

Parameters
xPositive scalar literal
Returns
1.0 / x in bj_real

Definition at line 86 of file math.h.

◆ bj_floor

#define bj_floor   bj_floorf

Floor.

Definition at line 239 of file math.h.

Referenced by bj_fract().

◆ bj_floord

#define bj_floord   floor

Floor (double)

Definition at line 154 of file math.h.

◆ bj_floorf

#define bj_floorf   floorf

Floor (float)

Definition at line 129 of file math.h.

◆ bj_floorl

#define bj_floorl   floorl

Floor (long double)

Definition at line 179 of file math.h.

◆ bj_fmod

#define bj_fmod   bj_fmodf

Floating modulus.

Definition at line 240 of file math.h.

Referenced by bj_mod().

◆ bj_fmodd

#define bj_fmodd   fmod

Floating modulus (double)

Definition at line 155 of file math.h.

◆ bj_fmodf

#define bj_fmodf   fmodf

Floating modulus (float)

Definition at line 130 of file math.h.

◆ bj_fmodl

#define bj_fmodl   fmodl

Floating modulus (long double)

Definition at line 180 of file math.h.

◆ BJ_FZERO

◆ bj_log

#define bj_log   bj_logf

Natural logarithm.

Definition at line 241 of file math.h.

◆ bj_logd

#define bj_logd   log

Natural log (double)

Definition at line 156 of file math.h.

◆ bj_logf

#define bj_logf   logf

Natural log (float)

Definition at line 131 of file math.h.

◆ bj_logl

#define bj_logl   logl

Natural log (long double)

Definition at line 181 of file math.h.

◆ BJ_M3

◆ BJ_M32

#define BJ_M32 ( c,
r )
Value:
((c)*2 + (r))

Index a 3×2 matrix element at column c, row r. 0 <= c < 3, 0 <= r < 2.

Definition at line 72 of file mat.h.

Referenced by bj_mat3_from_mat3x2(), bj_mat3x2_from_mat3(), bj_mat3x2_mul(), bj_mat3x2_set_identity(), bj_mat3x2_set_rotation_z(), bj_mat3x2_set_scaling_xy(), bj_mat3x2_set_translation(), bj_mat3x2_transform_dir(), and bj_mat3x2_transform_point().

◆ BJ_M4

◆ BJ_M43

#define BJ_M43 ( c,
r )

◆ bj_max

#define bj_max   bj_maxf

Maximum of two floats.

Definition at line 242 of file math.h.

Referenced by bj_real_is_zero_scaled(), and bj_real_relative_scale().

◆ bj_maxd

#define bj_maxd   fmax

Maximum of two doubles.

Definition at line 157 of file math.h.

◆ bj_maxf

#define bj_maxf   fmaxf

Maximum of two floats.

Definition at line 132 of file math.h.

◆ bj_maxl

#define bj_maxl   fmaxl

Maximum of two long doubles.

Definition at line 182 of file math.h.

◆ bj_min

#define bj_min   bj_minf

Minimum of two floats.

Definition at line 243 of file math.h.

◆ bj_mind

#define bj_mind   fmin

Minimum of two doubles.

Definition at line 158 of file math.h.

◆ bj_minf

#define bj_minf   fminf

Minimum of two floats.

Definition at line 133 of file math.h.

◆ bj_minl

#define bj_minl   fminl

Minimum of two long doubles.

Definition at line 183 of file math.h.

◆ BJ_PI

#define BJ_PI   (BJ_F(3.141592653589793238462643383279502884))

PI in the selected bj_real precision.

Examples
physics_kinematics.c.

Definition at line 111 of file math.h.

Referenced by reset_ball().

◆ BJ_PI_D

#define BJ_PI_D   (3.14159265358979323846264338327950288)

Double-precision PI.

Definition at line 103 of file math.h.

◆ BJ_PI_F

#define BJ_PI_F   (3.14159265358979323846f)

Single-precision PI.

Definition at line 99 of file math.h.

◆ BJ_PI_L

#define BJ_PI_L   (3.141592653589793238462643383279502884L)

Long-double PI.

Definition at line 107 of file math.h.

◆ bj_pow

#define bj_pow   bj_powf

Power.

Examples
physics_particle.c, and shaders.c.

Definition at line 244 of file math.h.

Referenced by bj_newton_plummer_gravitation(), orbital_speed_soft(), and shader_code().

◆ bj_powd

#define bj_powd   pow

Power (double)

Definition at line 159 of file math.h.

◆ bj_powf

#define bj_powf   powf

Power (float)

Definition at line 134 of file math.h.

◆ bj_powl

#define bj_powl   powl

Power (long double)

Definition at line 184 of file math.h.

◆ bj_round

#define bj_round   bj_roundf

Round to nearest integer.

Examples
random_distribution.c.

Definition at line 245 of file math.h.

Referenced by run_distributions().

◆ bj_roundd

#define bj_roundd   round

Round to nearest (double)

Definition at line 160 of file math.h.

◆ bj_roundf

#define bj_roundf   roundf

Round to nearest (float)

Definition at line 135 of file math.h.

◆ bj_roundl

#define bj_roundl   roundl

Round to nearest (long double)

Definition at line 185 of file math.h.

◆ bj_sin

◆ bj_sind

#define bj_sind   sin

Sine (double)

Definition at line 161 of file math.h.

◆ bj_sinf

#define bj_sinf   sinf

Sine (float)

Definition at line 136 of file math.h.

◆ bj_sinl

#define bj_sinl   sinl

Sine (long double)

Definition at line 186 of file math.h.

◆ bj_sqrt

◆ bj_sqrtd

#define bj_sqrtd   sqrt

Square root (double)

Definition at line 162 of file math.h.

◆ bj_sqrtf

#define bj_sqrtf   sqrtf

Square root (float)

Definition at line 137 of file math.h.

◆ bj_sqrtl

#define bj_sqrtl   sqrtl

Square root (long double)

Definition at line 187 of file math.h.

◆ bj_tan

#define bj_tan   bj_tanf

Tangent.

Definition at line 248 of file math.h.

Referenced by bj_mat4_set_perspective().

◆ bj_tand

#define bj_tand   tan

Tangent (double)

Definition at line 163 of file math.h.

◆ bj_tanf

#define bj_tanf   tanf

Tangent (float)

Definition at line 138 of file math.h.

◆ bj_tanl

#define bj_tanl   tanl

Tangent (long double)

Definition at line 188 of file math.h.

◆ BJ_TAU

#define BJ_TAU   (BJ_F(6.283185307179586476925286766559005768))

TAU in the selected bj_real precision.

Examples
physics_particle.c.

Definition at line 113 of file math.h.

Referenced by init_asteroids().

◆ BJ_TAU_D

#define BJ_TAU_D   (6.28318530717958647692528676655900576)

Double-precision TAU (2 * PI).

Definition at line 105 of file math.h.

◆ BJ_TAU_F

#define BJ_TAU_F   (6.28318530717958647692f)

Single-precision TAU (2 * PI).

Definition at line 101 of file math.h.

◆ BJ_TAU_L

#define BJ_TAU_L   (6.283185307179586476925286766559005768L)

Long-double TAU (2 * PI).

Definition at line 109 of file math.h.

◆ BJ_VEC2_ZERO

#define BJ_VEC2_ZERO   ((struct bj_vec2){BJ_FZERO, BJ_FZERO})

Zero 2D vector literal {0, 0}.

Examples
physics_particle.c.

Definition at line 57 of file vec.h.

Referenced by init_asteroids(), and init_planet().

◆ BJ_VEC3_ZERO

#define BJ_VEC3_ZERO   ((struct bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})

Zero 3D vector literal {0, 0, 0}.

Definition at line 71 of file vec.h.

◆ BJ_VEC4_ZERO

#define BJ_VEC4_ZERO   ((struct bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})

Zero 4D vector literal {0, 0, 0, 0}.

Definition at line 87 of file vec.h.

Typedef Documentation

◆ bj_real

typedef float bj_real

Selected real type for float configuration.

Examples
physics_kinematics.c, physics_particle.c, random_distribution.c, and shaders.c.

Definition at line 76 of file math.h.

Function Documentation

◆ bj_clamp()

bj_real bj_clamp ( bj_real x,
bj_real lo,
bj_real hi )
inlinestatic

Clamp x to the closed interval [lo, hi].

Parameters
xInput value
loLower bound
hiUpper bound
Returns
lo if x < lo, hi if x > hi, else x

Definition at line 266 of file math.h.

◆ bj_fract()

bj_real bj_fract ( bj_real x)
inlinestatic

Fractional part of x.

Parameters
xInput value
Returns
x - floor(x)
Examples
shaders.c.

Definition at line 298 of file math.h.

Referenced by shader_code().

◆ bj_mat3_add()

void bj_mat3_add ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A,
const struct bj_mat3x3 *restrict B )
inlinestatic

Component-wise addition: out = A + B.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix.
BInput 3×3 matrix.

Definition at line 170 of file mat.h.

◆ bj_mat3_col()

struct bj_vec3 bj_mat3_col ( const struct bj_mat3x3 *restrict M,
int c )
inlinestatic

Extract a matrix column as a vector.

Parameters
MInput 3×3 matrix.
cindex (0-based).
Returns
3D vector result.

Definition at line 137 of file mat.h.

Referenced by bj_mat3_col().

◆ bj_mat3_copy()

void bj_mat3_copy ( struct bj_mat3x3 *restrict dst,
const struct bj_mat3x3 *restrict src )
inlinestatic

Copy a matrix.

Parameters
dstOutput 3×3 matrix.
srcInput 3×3 matrix.

Definition at line 109 of file mat.h.

◆ bj_mat3_determinant()

bj_real bj_mat3_determinant ( const struct bj_mat3x3 *restrict A)
inlinestatic

Determinant of a 3×3 matrix.

Parameters
AInput 3×3 matrix.
Returns
bj_real.

Definition at line 383 of file mat.h.

◆ bj_mat3_from_mat3x2()

void bj_mat3_from_mat3x2 ( struct bj_mat3x3 *restrict M,
const struct bj_mat3x2 *restrict A )
inlinestatic

Promote a 3×2 affine matrix to 3×3.

Parameters
MOutput 3×3 matrix.
AInput 3×2 matrix.

Definition at line 689 of file mat.h.

◆ bj_mat3_invert()

bj_bool bj_mat3_invert ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A )
inlinestatic

Invert a 3×3 matrix (safe, adjugate).

Computes A^{-1} = adj(A) / det(A). If det(A) is zero by bj_real_is_zero, returns 0 and leaves out unspecified.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix.
Returns
BJ_TRUE on success, BJ_FALSE if singular or near-singular by bj_real_is_zero(det).
Warning
Adj/Det method is numerically unstable for ill-conditioned A.
See also
bj_mat3_invert_unsafe

Definition at line 407 of file mat.h.

◆ bj_mat3_invert_unsafe()

void bj_mat3_invert_unsafe ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A )
inlinestatic

Invert a 3×3 matrix (unsafe, adjugate).

Same as bj_mat3_invert but skips the singularity check and divides by det(A) unconditionally. Faster when invertibility is guaranteed.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix (must be nonsingular).
Precondition
det(A) != 0 and A is well-conditioned for numerical stability.
Warning
Division by zero if det(A) == 0. Consider bj_mat3_invert instead.

Definition at line 444 of file mat.h.

◆ bj_mat3_mul()

void bj_mat3_mul ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A,
const struct bj_mat3x3 *restrict B )
inlinestatic

Matrix product: out = A * B.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix.
BInput 3×3 matrix.
Note
Column-major storage. Vectors are columns. Right-multiply: r = M * v.
Examples
physics_particle.c.

Definition at line 219 of file mat.h.

Referenced by update_projection().

◆ bj_mat3_mul_scalar()

void bj_mat3_mul_scalar ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A,
bj_real k )
inlinestatic

Scalar multiply: out = A * k.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix.
kUniform scale factor.

Definition at line 202 of file mat.h.

◆ bj_mat3_row()

struct bj_vec3 bj_mat3_row ( const struct bj_mat3x3 *restrict M,
int r )
inlinestatic

Extract a matrix row as a vector.

Parameters
MInput 3×3 matrix.
rindex (0-based).
Returns
3D vector result.

Definition at line 124 of file mat.h.

Referenced by bj_mat3_row(), and bj_mat3_translate().

◆ bj_mat3_set_identity()

void bj_mat3_set_identity ( struct bj_mat3x3 *restrict M)
inlinestatic

Set a 3×3 matrix to identity.

Parameters
MOutput 3×3 matrix.

Definition at line 97 of file mat.h.

Referenced by bj_mat3_set_scaling_xy(), bj_mat3_set_shear_xy(), and bj_mat3_set_translation().

◆ bj_mat3_set_ortho()

void bj_mat3_set_ortho ( struct bj_mat3x3 *restrict M,
bj_real l,
bj_real r,
bj_real b,
bj_real t )
inlinestatic

Build a 2D orthographic projection into a 3×3 matrix.

Maps the rectangle [l, r] × [b, t] in source space to the [-1, 1] × [-1, 1] normalised range. Y is inverted relative to a top-left-origin source.

Parameters
MOutput 3×3 matrix.
lLeft edge of the source rectangle.
rRight edge of the source rectangle (must differ from l).
bBottom edge of the source rectangle.
tTop edge of the source rectangle (must differ from b).
Examples
physics_particle.c.

Definition at line 479 of file mat.h.

Referenced by update_projection().

◆ bj_mat3_set_rotation_z()

void bj_mat3_set_rotation_z ( struct bj_mat3x3 *restrict M,
bj_real angle )
inlinestatic

Build a Z-axis (i.e.

2D) rotation into a 3×3 matrix.

Parameters
MOutput 3×3 matrix.
angleRotation angle in radians.

Definition at line 360 of file mat.h.

◆ bj_mat3_set_scaling_xy()

void bj_mat3_set_scaling_xy ( struct bj_mat3x3 *restrict M,
bj_real sx,
bj_real sy )
inlinestatic

Build a non-uniform XY scale into a 3×3 matrix.

Overwrites M with the identity, then sets the X and Y scale on the diagonal. Use bj_mat3_mul to apply this to an existing transform.

Parameters
MOutput 3×3 matrix.
sxScale on X.
syScale on Y.

Definition at line 326 of file mat.h.

◆ bj_mat3_set_shear_xy()

void bj_mat3_set_shear_xy ( struct bj_mat3x3 *restrict M,
bj_real shx,
bj_real shy )
inlinestatic

Build an XY shear into a 3×3 matrix.

Overwrites M with the identity, then sets the shear coefficients.

Parameters
MOutput 3×3 matrix.
shxShear along X proportional to Y (column 1, row 0).
shyShear along Y proportional to X (column 0, row 1).

Definition at line 345 of file mat.h.

◆ bj_mat3_set_translation()

void bj_mat3_set_translation ( struct bj_mat3x3 *restrict M,
bj_real tx,
bj_real ty )
inlinestatic

Build a 3×3 translation matrix.

Parameters
MOutput 3×3 matrix.
txTranslation along x.
tyTranslation along y.

Definition at line 286 of file mat.h.

◆ bj_mat3_set_viewport()

void bj_mat3_set_viewport ( struct bj_mat3x3 *restrict M,
bj_real x,
bj_real y,
bj_real w,
bj_real h )
inlinestatic

Build a 2D viewport transform into a 3×3 matrix.

Parameters
MOutput 3×3 matrix.
xViewport X origin in pixels.
yViewport Y origin in pixels.
wViewport width in pixels.
hViewport height in pixels.
Examples
physics_particle.c.

Definition at line 506 of file mat.h.

Referenced by update_projection().

◆ bj_mat3_sub()

void bj_mat3_sub ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A,
const struct bj_mat3x3 *restrict B )
inlinestatic

Component-wise subtraction: out = A - B.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix.
BInput 3×3 matrix.

Definition at line 186 of file mat.h.

◆ bj_mat3_transform_point()

struct bj_vec2 bj_mat3_transform_point ( const struct bj_mat3x3 *restrict M,
struct bj_vec2 p )
inlinestatic

Transform a 2D point by a 3×3 homogeneous transform.

Parameters
MInput 3×3 matrix.
pInput point.
Returns
2D vector result.
Note
Homogeneous 2D. Applies projective divide if w ≠ 0.

Definition at line 264 of file mat.h.

Referenced by bj_mat3_transform_point().

◆ bj_mat3_transform_vec3()

struct bj_vec3 bj_mat3_transform_vec3 ( const struct bj_mat3x3 *restrict M,
struct bj_vec3 v )
inlinestatic

Multiply a 3×3 matrix by a 3D vector: r = M * v.

Parameters
MInput 3×3 matrix.
vInput vector.
Returns
3D vector result.
Note
Column-major storage. Vectors are columns. Right-multiply: r = M * v.
Examples
physics_particle.c.

Definition at line 245 of file mat.h.

Referenced by bj_mat3_transform_vec3(), and draw().

◆ bj_mat3_translate()

void bj_mat3_translate ( struct bj_mat3x3 *restrict M,
bj_real tx,
bj_real ty )
inlinestatic

Right-multiply by a translation: M = M * T(tx,ty).

Parameters
MOutput 3×3 matrix.
txTranslation along x.
tyTranslation along y.
Note
Right-multiply in place.

Definition at line 303 of file mat.h.

◆ bj_mat3_transpose()

void bj_mat3_transpose ( struct bj_mat3x3 *restrict out,
const struct bj_mat3x3 *restrict A )
inlinestatic

Transpose a matrix.

Parameters
outOutput 3×3 matrix.
AInput 3×3 matrix.

Definition at line 147 of file mat.h.

◆ bj_mat3x2_from_mat3()

void bj_mat3x2_from_mat3 ( struct bj_mat3x2 *restrict M,
const struct bj_mat3x3 *restrict A )
inlinestatic

Demote a 3×3 matrix to 3×2 (drop projective terms).

Parameters
MOutput 3×2 matrix.
AInput 3×3 matrix.

Definition at line 711 of file mat.h.

◆ bj_mat3x2_mul()

void bj_mat3x2_mul ( struct bj_mat3x2 *restrict out,
const struct bj_mat3x2 *restrict A,
const struct bj_mat3x2 *restrict B )
inlinestatic

Matrix product: out = A * B.

Parameters
outOutput 3×2 matrix.
AInput 3×2 matrix.
BInput 3×2 matrix.
Note
Column-major storage. Vectors are columns. Right-multiply: r = M * v.

Definition at line 607 of file mat.h.

◆ bj_mat3x2_set_identity()

void bj_mat3x2_set_identity ( struct bj_mat3x2 *restrict M)
inlinestatic

Set a 3×2 affine matrix to identity.

Parameters
MOutput 3×2 matrix.

Definition at line 529 of file mat.h.

Referenced by bj_mat3x2_set_scaling_xy(), and bj_mat3x2_set_translation().

◆ bj_mat3x2_set_rotation_z()

void bj_mat3x2_set_rotation_z ( struct bj_mat3x2 *restrict M,
bj_real angle )
inlinestatic

Build a 3×2 Z-axis (2D) rotation.

Parameters
MOutput 3×2 matrix.
angleRotation angle in radians.

Definition at line 585 of file mat.h.

◆ bj_mat3x2_set_scaling_xy()

void bj_mat3x2_set_scaling_xy ( struct bj_mat3x2 *restrict M,
bj_real sx,
bj_real sy )
inlinestatic

Build a 3×2 non-uniform XY scale.

Overwrites M with the identity and sets the X and Y scale on the diagonal of the linear block.

Parameters
MOutput 3×2 matrix.
sxScale on X.
syScale on Y.

Definition at line 570 of file mat.h.

◆ bj_mat3x2_set_translation()

void bj_mat3x2_set_translation ( struct bj_mat3x2 *restrict M,
bj_real tx,
bj_real ty )
inlinestatic

Build a 3×2 translation matrix.

Overwrites M with the identity and sets the translation column.

Parameters
MOutput 3×2 matrix.
txTranslation along X.
tyTranslation along Y.

Definition at line 550 of file mat.h.

◆ bj_mat3x2_transform_dir()

struct bj_vec2 bj_mat3x2_transform_dir ( const struct bj_mat3x2 *restrict M,
struct bj_vec2 v )
inlinestatic

Transform a direction vector by a 3×2 affine matrix (ignoring translation).

Applies the 2×2 linear block only. Use for directions, normals (when the linear part is rigid/orthogonal), and offsets: anything that shouldn't pick up the translation column.

Parameters
MInput 3×2 matrix.
vInput direction vector.
Returns
Transformed direction.
See also
bj_mat3x2_transform_point

Definition at line 671 of file mat.h.

Referenced by bj_mat3x2_transform_dir().

◆ bj_mat3x2_transform_point()

struct bj_vec2 bj_mat3x2_transform_point ( const struct bj_mat3x2 *restrict M,
struct bj_vec2 p )
inlinestatic

Transform a 2D point by a 3×2 affine matrix.

Applies the 2×2 linear block and adds the translation column. No projective divide: bj_mat3x2 has no projective term.

Parameters
MInput 3×2 matrix.
pInput point.
Returns
Transformed point.
See also
bj_mat3x2_transform_dir, bj_mat3_transform_point

Definition at line 646 of file mat.h.

Referenced by bj_mat3x2_transform_point().

◆ bj_mat4_add()

void bj_mat4_add ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A,
const struct bj_mat4x4 *restrict B )
inlinestatic

Component-wise addition: out = A + B.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.
BInput 4×4 matrix.

Definition at line 830 of file mat.h.

◆ bj_mat4_col()

struct bj_vec4 bj_mat4_col ( const struct bj_mat4x4 *restrict M,
int c )
inlinestatic

Extract a matrix column as a vector.

Parameters
MInput 4×4 matrix.
cindex (0-based).
Returns
4D vector result.

Definition at line 790 of file mat.h.

Referenced by bj_mat4_col().

◆ bj_mat4_copy()

void bj_mat4_copy ( struct bj_mat4x4 *restrict dst,
const struct bj_mat4x4 *restrict src )
inlinestatic

Copy a matrix.

Parameters
dstOutput 4×4 matrix.
srcInput 4×4 matrix.

Definition at line 756 of file mat.h.

Referenced by bj_mat4_orthonormalize(), and bj_mat4_rotate_axis_angle().

◆ bj_mat4_from_mat4x3()

void bj_mat4_from_mat4x3 ( struct bj_mat4x4 *restrict M,
const struct bj_mat4x3 *restrict A )
inlinestatic

Promote a 4×3 affine matrix to 4×4.

Parameters
MOutput 4×4 matrix.
AInput 4×3 matrix.

Definition at line 1854 of file mat.h.

◆ bj_mat4_invert()

bj_bool bj_mat4_invert ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict M )
inlinestatic

Invert a 4×4 matrix (safe).

Computes A^{-1} via cofactors. If det(A) is zero by bj_real_is_zero, returns 0 and leaves out unspecified.

Parameters
outOutput 4×4 matrix.
MInput 4×4 matrix.
Returns
BJ_TRUE on success, BJ_FALSE if singular or near-singular by bj_real_is_zero(det).
Warning
Cofactor-based inversion is costly and can be unstable for ill-conditioned matrices. Prefer affine/orthonormal specializations when possible.
See also
bj_mat4_invert_unsafe

Definition at line 1285 of file mat.h.

◆ bj_mat4_invert_unsafe()

void bj_mat4_invert_unsafe ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict M )
inlinestatic

Invert a 4×4 matrix (unsafe).

Same as bj_mat4_invert but skips the singularity check and divides by det(A) unconditionally.

Parameters
outOutput 4×4 matrix.
MInput 4×4 matrix (must be nonsingular).
Precondition
det(M) != 0 and M is well-conditioned for numerical stability.
Warning
Division by zero if det(M) == 0. Consider bj_mat4_invert instead.

Definition at line 1349 of file mat.h.

◆ bj_mat4_mul()

void bj_mat4_mul ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A,
const struct bj_mat4x4 *restrict B )
inlinestatic

Matrix product: out = A * B.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.
BInput 4×4 matrix.
Note
Column-major storage. Vectors are columns. Right-multiply: r = M * v.

Definition at line 918 of file mat.h.

◆ bj_mat4_mul_scalar()

void bj_mat4_mul_scalar ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A,
bj_real k )
inlinestatic

Scalar multiply: out = A * k.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.
kUniform scale factor.

Definition at line 864 of file mat.h.

◆ bj_mat4_orthonormalize()

void bj_mat4_orthonormalize ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A )
inlinestatic

Orthonormalize the 3×3 linear part of a 4×4 matrix.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.
Warning
If the 3×3 block is degenerate the result may contain NaNs.

Definition at line 1402 of file mat.h.

◆ bj_mat4_rotate_arcball()

void bj_mat4_rotate_arcball ( struct bj_mat4x4 *restrict R,
const struct bj_mat4x4 *restrict M,
struct bj_vec2 a,
struct bj_vec2 b,
bj_real s )
inlinestatic

Right-multiply by an arcball rotation between two screen points.

Lifts each 2D input onto the unit hemisphere (if outside the unit disc, it's normalised onto the equator instead), takes the rotation from A to B, and right-multiplies it into M, writing the result to R. Used to implement trackball-style camera controls driven by mouse drag.

Parameters
ROutput 4×4 matrix.
MInput 4×4 matrix.
aStart point in screen-normalised coordinates (typically [-1,1]^2).
bEnd point in screen-normalised coordinates.
sScalar speed multiplier applied to the rotation angle.

Definition at line 1242 of file mat.h.

◆ bj_mat4_rotate_axis_angle()

void bj_mat4_rotate_axis_angle ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict M,
struct bj_vec3 axis,
bj_real angle )
inlinestatic

Right-multiply a 4×4 by a rotation around an arbitrary axis.

Composes out = M * R(axis, angle). The axis is normalised internally; if its length is below 1e-4, the rotation is skipped and out = M.

Parameters
outOutput 4×4 matrix.
MInput 4×4 matrix.
axisAxis of rotation (need not be unit length).
angleRotation angle in radians.
See also
bj_mat4_rotate_x, bj_mat4_rotate_y, bj_mat4_rotate_z

Definition at line 1070 of file mat.h.

Referenced by bj_mat4_rotate_arcball().

◆ bj_mat4_rotate_x()

void bj_mat4_rotate_x ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict M,
bj_real a )
inlinestatic

Right-multiply a 4×4 by an X-axis rotation: out = M * Rx(a).

Parameters
outOutput 4×4 matrix.
MInput 4×4 matrix.
aRotation angle in radians.
See also
bj_mat4_rotate_y, bj_mat4_rotate_z, bj_mat4_rotate_axis_angle

Definition at line 1131 of file mat.h.

◆ bj_mat4_rotate_y()

void bj_mat4_rotate_y ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict M,
bj_real a )
inlinestatic

Right-multiply a 4×4 by a Y-axis rotation: out = M * Ry(a).

Parameters
outOutput 4×4 matrix.
MInput 4×4 matrix.
aRotation angle in radians.
See also
bj_mat4_rotate_x, bj_mat4_rotate_z, bj_mat4_rotate_axis_angle

Definition at line 1166 of file mat.h.

◆ bj_mat4_rotate_z()

void bj_mat4_rotate_z ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict M,
bj_real a )
inlinestatic

Right-multiply a 4×4 by a Z-axis rotation: out = M * Rz(a).

Parameters
outOutput 4×4 matrix.
MInput 4×4 matrix.
aRotation angle in radians.
See also
bj_mat4_rotate_x, bj_mat4_rotate_y, bj_mat4_rotate_axis_angle

Definition at line 1200 of file mat.h.

◆ bj_mat4_row()

struct bj_vec4 bj_mat4_row ( const struct bj_mat4x4 *restrict M,
int r )
inlinestatic

Extract a matrix row as a vector.

Parameters
MInput 4×4 matrix.
rindex (0-based).
Returns
4D vector result.

Definition at line 771 of file mat.h.

Referenced by bj_mat4_row(), and bj_mat4_translate().

◆ bj_mat4_scale_axes()

void bj_mat4_scale_axes ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A,
bj_real sx,
bj_real sy,
bj_real sz )
inlinestatic

Scale basis vectors of a 4×4 matrix by per-axis factors.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.
sxScale on x.
syScale on y.
szScale on z.

Definition at line 883 of file mat.h.

◆ bj_mat4_set_frustum()

void bj_mat4_set_frustum ( struct bj_mat4x4 *restrict M,
bj_real l,
bj_real r,
bj_real b,
bj_real t,
bj_real n,
bj_real f )
inlinestatic

Build a perspective frustum projection into a 4×4 matrix.

Maps the frustum bounded by [l, r] × [b, t] on the near plane (and the corresponding rays out to the far plane) to NDC. Depth maps to [0, 1]. Y is inverted relative to a top-left-origin source.

Parameters
MOutput 4×4 matrix.
lLeft edge on the near plane.
rRight edge on the near plane (must satisfy r > l).
bBottom edge on the near plane.
tTop edge on the near plane (must satisfy t > b).
nNear plane distance.
fFar plane distance (must satisfy f > n).
See also
bj_mat4_set_perspective, bj_mat4_set_ortho

Definition at line 1458 of file mat.h.

◆ bj_mat4_set_identity()

void bj_mat4_set_identity ( struct bj_mat4x4 *restrict M)
inlinestatic

Set a 4×4 matrix to identity.

Parameters
MOutput 4×4 matrix.

Definition at line 729 of file mat.h.

Referenced by bj_mat4_set_lookat(), bj_mat4_set_translation(), and bj_quat_to_mat4().

◆ bj_mat4_set_lookat()

void bj_mat4_set_lookat ( struct bj_mat4x4 *restrict M,
struct bj_vec3 eye,
struct bj_vec3 center,
struct bj_vec3 up )
inlinestatic

Build a right-handed look-at view matrix.

Parameters
MOutput 4×4 matrix.
eyeCamera position.
centerTarget point the camera looks at.
upApproximate up direction.
Note
Right-handed view: +Z looks from eye to center.

Definition at line 1608 of file mat.h.

◆ bj_mat4_set_ortho()

void bj_mat4_set_ortho ( struct bj_mat4x4 *restrict M,
bj_real l,
bj_real r,
bj_real b,
bj_real t,
bj_real n,
bj_real f )
inlinestatic

Build a 3D orthographic projection into a 4×4 matrix.

Maps the box [l, r] × [b, t] × [n, f] to NDC. Depth maps to [0, 1]. Y is inverted relative to a top-left-origin source.

Parameters
MOutput 4×4 matrix.
lLeft edge of the source box.
rRight edge of the source box (must differ from l).
bBottom edge of the source box.
tTop edge of the source box (must differ from b).
nNear plane distance.
fFar plane distance (must differ from n).
See also
bj_mat4_set_frustum, bj_mat3_set_ortho

Definition at line 1501 of file mat.h.

◆ bj_mat4_set_outer_product()

void bj_mat4_set_outer_product ( struct bj_mat4x4 *restrict out,
struct bj_vec3 a,
struct bj_vec3 b )
inlinestatic

Build the outer product a * b^T as a 4×4 matrix.

Overwrites out with the 3×3 outer-product block embedded in a 4×4 zero matrix: out[i, j] = a[i] * b[j] for i, j < 3, all other entries zero. The 4×4 result has rank 1.

Parameters
outOutput 4×4 matrix.
aLeft vector (becomes the column of the outer product).
bRight vector (becomes the row of the outer product).

Definition at line 1033 of file mat.h.

◆ bj_mat4_set_perspective()

void bj_mat4_set_perspective ( struct bj_mat4x4 *restrict M,
bj_real y_fov,
bj_real aspect,
bj_real n,
bj_real f )
inlinestatic

Build a 4×4 perspective projection from vertical FOV.

Parameters
MOutput 4×4 matrix.
y_fovVertical field of view in radians.
aspectAspect ratio width/height.
nNear plane distance.
fFar plane distance.
Warning
Requires f > n and aspect > 0. Depth maps to [0,1]. Y is inverted.

Definition at line 1537 of file mat.h.

◆ bj_mat4_set_translation()

void bj_mat4_set_translation ( struct bj_mat4x4 *restrict M,
bj_real x,
bj_real y,
bj_real z )
inlinestatic

Build a 4×4 translation matrix.

Overwrites M with the identity and sets the translation column.

Parameters
MOutput 4×4 matrix.
xTranslation along X.
yTranslation along Y.
zTranslation along Z.

Definition at line 987 of file mat.h.

◆ bj_mat4_set_viewport()

void bj_mat4_set_viewport ( struct bj_mat4x4 *restrict M,
bj_real x,
bj_real y,
bj_real w,
bj_real h )
inlinestatic

Build a 3D viewport transform into a 4×4 matrix.

Parameters
MOutput 4×4 matrix.
xViewport X origin in pixels.
yViewport Y origin in pixels.
wViewport width in pixels.
hViewport height in pixels.
Note
Maps NDC x,y ∈ [-1,1] and z ∈ [0,1] to window coordinates.

Definition at line 1572 of file mat.h.

◆ bj_mat4_sub()

void bj_mat4_sub ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A,
const struct bj_mat4x4 *restrict B )
inlinestatic

Component-wise subtraction: out = A - B.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.
BInput 4×4 matrix.

Definition at line 847 of file mat.h.

◆ bj_mat4_transform_vec4()

struct bj_vec4 bj_mat4_transform_vec4 ( const struct bj_mat4x4 *restrict M,
struct bj_vec4 v )
inlinestatic

Multiply a 4×4 matrix by a 4D vector: r = M * v.

Parameters
MInput 4×4 matrix.
vInput vector.
Returns
4D vector result.
Note
Column-major storage. Vectors are columns. Right-multiply: r = M * v.

Definition at line 960 of file mat.h.

Referenced by bj_mat4_transform_vec4().

◆ bj_mat4_translate()

void bj_mat4_translate ( struct bj_mat4x4 *restrict M,
bj_real x,
bj_real y,
bj_real z )
inlinestatic

Right-multiply by a translation: M = M * T(x, y, z).

In place: M is overwritten with the composed result.

Parameters
MIn/out 4×4 matrix.
xTranslation along X.
yTranslation along Y.
zTranslation along Z.

Definition at line 1009 of file mat.h.

Referenced by bj_mat4_set_lookat().

◆ bj_mat4_transpose()

void bj_mat4_transpose ( struct bj_mat4x4 *restrict out,
const struct bj_mat4x4 *restrict A )
inlinestatic

Transpose a matrix.

Parameters
outOutput 4×4 matrix.
AInput 4×4 matrix.

Definition at line 808 of file mat.h.

◆ bj_mat4x3_from_mat4()

void bj_mat4x3_from_mat4 ( struct bj_mat4x3 *restrict M,
const struct bj_mat4x4 *restrict A )
inlinestatic

Demote a 4×4 matrix to 4×3 (drop projective terms).

Parameters
MOutput 4×3 matrix.
AInput 4×4 matrix.

Definition at line 1882 of file mat.h.

◆ bj_mat4x3_mul()

void bj_mat4x3_mul ( struct bj_mat4x3 *restrict out,
const struct bj_mat4x3 *restrict A,
const struct bj_mat4x3 *restrict B )
inlinestatic

Matrix product: out = A * B.

Parameters
outOutput 4×3 matrix.
AInput 4×3 matrix.
BInput 4×3 matrix.
Note
Column-major storage. Vectors are columns. Right-multiply: r = M * v.

Definition at line 1762 of file mat.h.

◆ bj_mat4x3_set_identity()

void bj_mat4x3_set_identity ( struct bj_mat4x3 *restrict M)
inlinestatic

Set a 4×3 affine matrix to identity.

Parameters
MOutput 4×3 matrix.

Definition at line 1635 of file mat.h.

Referenced by bj_mat4x3_set_rotation_x(), bj_mat4x3_set_rotation_y(), bj_mat4x3_set_rotation_z(), bj_mat4x3_set_scaling_xyz(), and bj_mat4x3_set_translation().

◆ bj_mat4x3_set_rotation_x()

void bj_mat4x3_set_rotation_x ( struct bj_mat4x3 *restrict M,
bj_real a )
inlinestatic

Build a 4×3 X-axis rotation.

Overwrites M with the identity and sets the rotation block.

Parameters
MOutput 4×3 matrix.
aRotation angle in radians.
See also
bj_mat4x3_set_rotation_y, bj_mat4x3_set_rotation_z

Definition at line 1707 of file mat.h.

◆ bj_mat4x3_set_rotation_y()

void bj_mat4x3_set_rotation_y ( struct bj_mat4x3 *restrict M,
bj_real a )
inlinestatic

Build a 4×3 Y-axis rotation.

Overwrites M with the identity and sets the rotation block.

Parameters
MOutput 4×3 matrix.
aRotation angle in radians.
See also
bj_mat4x3_set_rotation_x, bj_mat4x3_set_rotation_z

Definition at line 1726 of file mat.h.

◆ bj_mat4x3_set_rotation_z()

void bj_mat4x3_set_rotation_z ( struct bj_mat4x3 *restrict M,
bj_real a )
inlinestatic

Build a 4×3 Z-axis rotation.

Overwrites M with the identity and sets the rotation block.

Parameters
MOutput 4×3 matrix.
aRotation angle in radians.
See also
bj_mat4x3_set_rotation_x, bj_mat4x3_set_rotation_y

Definition at line 1745 of file mat.h.

◆ bj_mat4x3_set_scaling_xyz()

void bj_mat4x3_set_scaling_xyz ( struct bj_mat4x3 *restrict M,
bj_real sx,
bj_real sy,
bj_real sz )
inlinestatic

Build a 4×3 non-uniform XYZ scale.

Overwrites M with the identity and sets the X, Y, Z scale on the diagonal of the linear block.

Parameters
MOutput 4×3 matrix.
sxScale on X.
syScale on Y.
szScale on Z.

Definition at line 1686 of file mat.h.

◆ bj_mat4x3_set_translation()

void bj_mat4x3_set_translation ( struct bj_mat4x3 *restrict M,
bj_real tx,
bj_real ty,
bj_real tz )
inlinestatic

Build a 4×3 translation matrix.

Overwrites M with the identity and sets the translation column.

Parameters
MOutput 4×3 matrix.
txTranslation along X.
tyTranslation along Y.
tzTranslation along Z.

Definition at line 1663 of file mat.h.

◆ bj_mat4x3_transform_dir()

struct bj_vec3 bj_mat4x3_transform_dir ( const struct bj_mat4x3 *restrict M,
struct bj_vec3 v )
inlinestatic

Transform a direction vector (ignoring translation).

Parameters
MInput 4×3 matrix.
vInput vector.
Returns
3D vector result.
Note
Affine 3D. Ignores projective terms; no divide.

Definition at line 1837 of file mat.h.

Referenced by bj_mat4x3_transform_dir().

◆ bj_mat4x3_transform_point()

struct bj_vec3 bj_mat4x3_transform_point ( const struct bj_mat4x3 *restrict M,
struct bj_vec3 p )
inlinestatic

Transform a 3D point by a 4×3 affine matrix.

Parameters
MInput 4×3 matrix.
pInput point.
Returns
3D vector result.
Note
Affine 3D. Ignores projective terms; no divide.

Definition at line 1818 of file mat.h.

Referenced by bj_mat4x3_transform_point().

◆ bj_mod()

bj_real bj_mod ( bj_real x,
bj_real y )
inlinestatic

Positive modulus with non-negative result magnitude.

Parameters
xDividend
yDivisor (must be non-zero)
Returns
Remainder in [0, |y|) with the sign of y
Precondition
y != 0

Definition at line 309 of file math.h.

◆ bj_quat_conjugate()

struct bj_vec4 bj_quat_conjugate ( struct bj_vec4 q)
inlinestatic

Conjugate of a quaternion.

Negates the vector part and keeps the scalar part: conj(q) = {-x,-y,-z,w}.

Parameters
qQuaternion.
Returns
Conjugated quaternion.
See also
bj_quat_inverse

Definition at line 138 of file quat.h.

Referenced by bj_quat_conjugate().

◆ bj_quat_dot()

bj_real bj_quat_dot ( struct bj_vec4 a,
struct bj_vec4 b )
inlinestatic

4D dot product between two quaternions.

Parameters
aFirst quaternion.
bSecond quaternion.
Returns
a·b.
Note
For unit quaternions this equals cos(theta) where theta is the half-angle between orientations used by bj_quat_slerp.

Definition at line 73 of file quat.h.

Referenced by bj_quat_norm2(), and bj_quat_slerp().

◆ bj_quat_from_axis_angle()

struct bj_vec4 bj_quat_from_axis_angle ( struct bj_vec3 axis,
bj_real angle_rad )
inlinestatic

Build a quaternion from a rotation axis and angle.

Parameters
axisRotation axis. Need not be unit length.
angle_radRotation angle in radians.
Returns
Quaternion representing the rotation.
Note
Identity is returned if the axis length is near zero.

Definition at line 263 of file quat.h.

Referenced by bj_quat_from_axis_angle().

◆ bj_quat_from_mat4()

struct bj_vec4 bj_quat_from_mat4 ( const struct bj_mat4x4 *restrict M)
inlinestatic

Build a quaternion from a 4×4 rotation matrix.

Only the upper-left 3×3 block is used. Assumes it encodes a proper rotation. The result is normalised.

Parameters
MSource 4×4 matrix (column-major).
Returns
Quaternion representing the rotation.

Definition at line 374 of file quat.h.

Referenced by bj_quat_from_mat4().

◆ bj_quat_identity()

struct bj_vec4 bj_quat_identity ( void )
inlinestatic

Return the identity quaternion.

Represents a no-rotation. Equivalent to {0,0,0,1}.

Returns
Identity quaternion.
See also
bj_quat_normalize

Definition at line 57 of file quat.h.

Referenced by bj_quat_from_axis_angle(), bj_quat_identity(), bj_quat_inverse(), and bj_quat_normalize().

◆ bj_quat_inverse()

struct bj_vec4 bj_quat_inverse ( struct bj_vec4 q)
inlinestatic

Multiplicative inverse of a quaternion.

Returns identity if the squared norm is near zero (<= BJ_EPSILON). Otherwise q^{-1} = conj(q) / ||q||^2.

Parameters
qQuaternion.
Returns
Inverse quaternion.
See also
bj_quat_conjugate, bj_quat_normalize

Definition at line 156 of file quat.h.

Referenced by bj_quat_inverse().

◆ bj_quat_mul()

struct bj_vec4 bj_quat_mul ( struct bj_vec4 p,
struct bj_vec4 q )
inlinestatic

Hamilton product p * q.

Composition order follows standard Hamilton convention. When used to rotate vectors via v' = q * v * q^{-1}, apply q on the left.

Parameters
pLeft quaternion.
qRight quaternion.
Returns
Product p*q.

Definition at line 178 of file quat.h.

Referenced by bj_quat_mul().

◆ bj_quat_norm()

bj_real bj_quat_norm ( struct bj_vec4 q)
inlinestatic

Euclidean norm (length).

Parameters
qQuaternion.
Returns
||q||.

Definition at line 100 of file quat.h.

◆ bj_quat_norm2()

bj_real bj_quat_norm2 ( struct bj_vec4 q)
inlinestatic

Squared Euclidean norm.

Parameters
qQuaternion.
Returns
||q||^2.
See also
bj_quat_norm, bj_quat_normalize

Definition at line 88 of file quat.h.

Referenced by bj_quat_inverse(), bj_quat_norm(), and bj_quat_normalize().

◆ bj_quat_normalize()

struct bj_vec4 bj_quat_normalize ( struct bj_vec4 q)
inlinestatic

Normalise a quaternion.

Returns identity if the input length is near zero (<= BJ_EPSILON).

Parameters
qQuaternion.
Returns
Unit-length quaternion.

Definition at line 115 of file quat.h.

Referenced by bj_quat_from_mat4(), bj_quat_normalize(), bj_quat_slerp(), and bj_quat_to_mat4().

◆ bj_quat_rotate_vec3()

struct bj_vec3 bj_quat_rotate_vec3 ( struct bj_vec4 q,
struct bj_vec3 v )
inlinestatic

Rotate a 3D vector by a quaternion.

Parameters
qRotation quaternion. Expected to be unit length for pure rotation.
vVector to rotate.
Returns
Rotated vector.
Note
For performance, q is not normalised inside the function. Call bj_quat_normalize beforehand if needed.

Definition at line 291 of file quat.h.

Referenced by bj_quat_rotate_vec3(), and bj_quat_rotate_vec4().

◆ bj_quat_rotate_vec4()

struct bj_vec4 bj_quat_rotate_vec4 ( struct bj_vec4 q,
struct bj_vec4 v )
inlinestatic

Rotate a 4D vector by a quaternion, preserving w.

Parameters
qRotation quaternion. Expected to be unit length for pure rotation.
vVector to rotate. Its w component is passed through unchanged.
Returns
Rotated vector with original w.

Definition at line 313 of file quat.h.

Referenced by bj_quat_rotate_vec4().

◆ bj_quat_slerp()

struct bj_vec4 bj_quat_slerp ( struct bj_vec4 a,
struct bj_vec4 b,
bj_real t )
inlinestatic

Spherical linear interpolation between two orientations.

Interpolates along the shortest arc on S^3. If inputs are nearly parallel, falls back to normalised linear interpolation to avoid divide-by-zero.

Parameters
aStart quaternion.
bEnd quaternion.
tInterpolation factor in [0,1].
Returns
Interpolated quaternion.
Note
Inputs need not be normalised; the result is normalised.

Definition at line 204 of file quat.h.

Referenced by bj_quat_slerp().

◆ bj_quat_to_mat4()

void bj_quat_to_mat4 ( struct bj_mat4x4 *restrict M,
struct bj_vec4 q )
inlinestatic

Fill a 4×4 rotation matrix from a quaternion.

Parameters
[out]MDestination matrix (column-major).
[in]qInput quaternion. It is normalised internally.
Note
The resulting matrix has the last row and column set to form a proper rigid transform rotation block with bottom-right element equal to 1.

Definition at line 330 of file quat.h.

◆ bj_real_cmp()

int bj_real_cmp ( bj_real a,
bj_real b )
inlinestatic

Three-way compare using absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
-1 if a < b, 1 if a > b, 0 otherwise

Definition at line 390 of file math.h.

◆ bj_real_cmp_rel()

int bj_real_cmp_rel ( bj_real a,
bj_real b )
inlinestatic

Three-way compare using relative epsilon.

Parameters
aFirst value
bSecond value
Returns
-1 if a < b, 1 if a > b, 0 otherwise

Definition at line 464 of file math.h.

◆ bj_real_eq()

int bj_real_eq ( bj_real a,
bj_real b )
inlinestatic

Equality within absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if |a - b| <= BJ_EPSILON

Definition at line 342 of file math.h.

Referenced by bj_real_neq().

◆ bj_real_eq_rel()

int bj_real_eq_rel ( bj_real a,
bj_real b )
inlinestatic

Equality within relative epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if |a - b| <= BJ_EPSILON * max(1, |a|, |b|)

Definition at line 416 of file math.h.

Referenced by bj_real_neq_rel().

◆ bj_real_gt()

int bj_real_gt ( bj_real a,
bj_real b )
inlinestatic

a > b by more than absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if (a - b) > BJ_EPSILON

Definition at line 366 of file math.h.

Referenced by bj_real_cmp(), and bj_real_lte().

◆ bj_real_gt_rel()

int bj_real_gt_rel ( bj_real a,
bj_real b )
inlinestatic

a > b by more than relative epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if (a - b) > BJ_EPSILON * max(1, |a|, |b|)

Definition at line 440 of file math.h.

Referenced by bj_real_cmp_rel(), and bj_real_lte_rel().

◆ bj_real_gte()

int bj_real_gte ( bj_real a,
bj_real b )
inlinestatic

a >= b within absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if a >= b within tolerance

Definition at line 382 of file math.h.

◆ bj_real_gte_rel()

int bj_real_gte_rel ( bj_real a,
bj_real b )
inlinestatic

a >= b within relative epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if a >= b within tolerance

Definition at line 456 of file math.h.

◆ bj_real_is_zero()

int bj_real_is_zero ( bj_real x)
inlinestatic

Absolute-zero test.

Parameters
xInput value
Returns
Non-zero if |x| <= BJ_EPSILON

Definition at line 479 of file math.h.

Referenced by bj_mat3_invert(), bj_mat4_invert(), bj_real_snap_zero(), bj_real_snorm_safe(), bj_vec2_normalize(), bj_vec2_scale_to_len(), bj_vec3_normalize(), bj_vec3_scale_to_len(), and bj_vec4_normalize().

◆ bj_real_is_zero_scaled()

int bj_real_is_zero_scaled ( bj_real x,
bj_real scale )
inlinestatic

Scaled zero test using max(1, |scale|).

Parameters
xInput value
scaleScale reference
Returns
Non-zero if |x| <= BJ_EPSILON * max(1, |scale|)

Definition at line 487 of file math.h.

◆ bj_real_lt()

int bj_real_lt ( bj_real a,
bj_real b )
inlinestatic

a < b by more than absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if (b - a) > BJ_EPSILON

Definition at line 358 of file math.h.

Referenced by bj_real_cmp(), and bj_real_gte().

◆ bj_real_lt_rel()

int bj_real_lt_rel ( bj_real a,
bj_real b )
inlinestatic

a < b by more than relative epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if (b - a) > BJ_EPSILON * max(1, |a|, |b|)

Definition at line 432 of file math.h.

Referenced by bj_real_cmp_rel(), and bj_real_gte_rel().

◆ bj_real_lte()

int bj_real_lte ( bj_real a,
bj_real b )
inlinestatic

a <= b within absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if a <= b within tolerance

Definition at line 374 of file math.h.

◆ bj_real_lte_rel()

int bj_real_lte_rel ( bj_real a,
bj_real b )
inlinestatic

a <= b within relative epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if a <= b within tolerance

Definition at line 448 of file math.h.

◆ bj_real_neq()

int bj_real_neq ( bj_real a,
bj_real b )
inlinestatic

Inequality within absolute epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if not equal within absolute epsilon

Definition at line 350 of file math.h.

◆ bj_real_neq_rel()

int bj_real_neq_rel ( bj_real a,
bj_real b )
inlinestatic

Inequality within relative epsilon.

Parameters
aFirst value
bSecond value
Returns
Non-zero if not equal within relative epsilon

Definition at line 424 of file math.h.

◆ bj_real_relative_scale()

bj_real bj_real_relative_scale ( bj_real a,
bj_real b )
inlinestatic

Internal scale helper max(1, |a|, |b|).

Parameters
aFirst value
bSecond value
Returns
Scale factor s >= 1

Definition at line 406 of file math.h.

Referenced by bj_real_eq_rel(), bj_real_gt_rel(), and bj_real_lt_rel().

◆ bj_real_snap_zero()

bj_real bj_real_snap_zero ( bj_real x)
inlinestatic

Snap to exact zero under absolute epsilon.

Parameters
xInput value
Returns
0 if |x| <= BJ_EPSILON, else x

Definition at line 497 of file math.h.

◆ bj_real_snorm_safe()

bj_real bj_real_snorm_safe ( bj_real x,
bj_real len )
inlinestatic

Safe scalar normalization.

Parameters
xNumerator
lenDenominator magnitude
Returns
0 if len is zero, else x / len

Definition at line 505 of file math.h.

◆ bj_rect_intersection()

bj_bool bj_rect_intersection ( const struct bj_rect * rect_a,
const struct bj_rect * rect_b,
struct bj_rect * result )

Compute the intersection of two rectangles.

Useful for clipping a drawing operation to a viewport, or for testing whether two on-screen regions overlap.

struct bj_rect viewport = {0, 0, 320, 240};
struct bj_rect sprite = {300, 100, 64, 64};
struct bj_rect clipped;
if (bj_rect_intersection(&viewport, &sprite, &clipped)) {
/* clipped is {300, 100, 20, 64}, the visible part of sprite *&zwj;/
}
bj_bool bj_rect_intersection(const struct bj_rect *rect_a, const struct bj_rect *rect_b, struct bj_rect *result)
Compute the intersection of two rectangles.
Axis-aligned rectangle: a top-left corner plus a width and height.
Definition rect.h:33
Parameters
rect_aPointer to the first rectangle. Must not be NULL.
rect_bPointer to the second rectangle. Must not be NULL.
resultPointer to receive the intersection, or NULL to only test for overlap without computing it.
Returns
BJ_TRUE if the rectangles overlap (and result, if non-NULL, was filled in); BJ_FALSE if they don't overlap or if either input is NULL.

◆ bj_rect_union()

void bj_rect_union ( const struct bj_rect * rect_a,
const struct bj_rect * rect_b,
struct bj_rect * result )

Compute the bounding box that contains both rectangles.

Useful for accumulating damage rectangles (each invalidate enlarges the bounding region) and similar set-of-pixels-affected bookkeeping.

struct bj_rect a = {10, 10, 20, 20};
struct bj_rect b = {25, 5, 10, 30};
struct bj_rect out;
bj_rect_union(&a, &b, &out); /* out is {10, 5, 25, 30} *&zwj;/
void bj_rect_union(const struct bj_rect *rect_a, const struct bj_rect *rect_b, struct bj_rect *result)
Compute the bounding box that contains both rectangles.

A zero-sized input rectangle contributes nothing to the union; a NULL input is treated the same way. If both inputs are zero-sized or NULL, result is set to the empty rectangle {0,0,0,0}.

result may alias either input pointer for in-place accumulation.

Parameters
rect_aPointer to the first rectangle, or NULL.
rect_bPointer to the second rectangle, or NULL.
resultPointer to receive the bounding box. Must not be NULL.

◆ bj_smoothstep()

bj_real bj_smoothstep ( bj_real e0,
bj_real e1,
bj_real x )
inlinestatic

Smooth Hermite step between e0 and e1.

Clamps t to [0,1].

Parameters
e0Start edge
e1End edge
xInput value
Returns
Interpolant in [0,1]

Definition at line 287 of file math.h.

◆ bj_step()

bj_real bj_step ( bj_real edge,
bj_real x )
inlinestatic

Step function.

Parameters
edgeThreshold edge
xInput value
Returns
0 if x < edge, else 1

Definition at line 276 of file math.h.

◆ bj_vec2_add()

struct bj_vec2 bj_vec2_add ( struct bj_vec2 lhs,
struct bj_vec2 rhs )
inlinestatic

Component-wise addition: lhs + rhs.

Parameters
lhsLeft-hand vector.
rhsRight-hand vector.
Returns
Sum.
See also
bj_vec2_sub, bj_vec2_add_scaled

Definition at line 115 of file vec.h.

Referenced by bj_vec2_add().

◆ bj_vec2_add_scaled()

struct bj_vec2 bj_vec2_add_scaled ( struct bj_vec2 lhs,
struct bj_vec2 rhs,
bj_real s )
inlinestatic

Fused scaled add: lhs + s * rhs.

Common building block for physics integration (pos += dt * vel), linear interpolation, and weighted sums.

Parameters
lhsBase vector.
rhsVector to add (after scaling).
sScalar weight applied to rhs.
Returns
lhs + s * rhs.
See also
bj_vec2_add, bj_vec2_scale

Definition at line 131 of file vec.h.

Referenced by bj_vec2_add_scaled().

◆ bj_vec2_distance()

bj_real bj_vec2_distance ( const struct bj_vec2 a,
const struct bj_vec2 b )
inlinestatic

Euclidean distance: ||a - b||.

Parameters
aFirst vector.
bSecond vector.
Returns
Distance.
See also
bj_vec2_distance_sq

Definition at line 268 of file vec.h.

◆ bj_vec2_distance_sq()

bj_real bj_vec2_distance_sq ( struct bj_vec2 a,
struct bj_vec2 b )
inlinestatic

Squared Euclidean distance: ||a - b||^2.

Faster than bj_vec2_distance. No square root. Prefer this when you only need to compare distances or test against a squared threshold (d2 < r*r).

Parameters
aFirst vector.
bSecond vector.
Returns
Squared distance.
See also
bj_vec2_distance

Definition at line 255 of file vec.h.

Referenced by bj_vec2_distance().

◆ bj_vec2_dot()

bj_real bj_vec2_dot ( struct bj_vec2 a,
struct bj_vec2 b )
inlinestatic

Dot product: a.x*b.x + a.y*b.y.

Parameters
aFirst vector.
bSecond vector.
Returns
Scalar dot product.
See also
bj_vec2_perp_dot, bj_vec2_len

Definition at line 189 of file vec.h.

Referenced by bj_mat4_rotate_arcball().

◆ bj_vec2_len()

bj_real bj_vec2_len ( struct bj_vec2 v)
inlinestatic

Euclidean length: sqrt(x^2 + y^2).

Parameters
vInput vector.
Returns
Length.
See also
bj_vec2_distance, bj_vec2_normalize
Examples
shaders.c.

Definition at line 219 of file vec.h.

Referenced by bj_mat4_rotate_arcball(), bj_vec2_scale_to_len(), and shader_code().

◆ bj_vec2_map()

struct bj_vec2 bj_vec2_map ( struct bj_vec2 a,
bj_real(* )(bj_real) )
inlinestatic

Apply a scalar function component-wise: (f(x), f(y)).

Use to lift any bj_real -> bj_real mapping (e.g. bj_sqrt, bj_abs, bj_floor, a user clamp) over a vector.

Parameters
aInput vector.
fFunction to apply to each component.
Returns
Vector with f applied to each component.
See also
bj_vec3_map, bj_vec4_map
Examples
shaders.c.

Definition at line 101 of file vec.h.

Referenced by bj_vec2_map(), and shader_code().

◆ bj_vec2_max()

struct bj_vec2 bj_vec2_max ( struct bj_vec2 a,
struct bj_vec2 b )
inlinestatic

Component-wise maximum: (max(a.x,b.x), max(a.y,b.y)).

Parameters
aFirst vector.
bSecond vector.
Returns
Component-wise maximum.
See also
bj_vec2_min

Definition at line 333 of file vec.h.

Referenced by bj_vec2_max().

◆ bj_vec2_min()

struct bj_vec2 bj_vec2_min ( struct bj_vec2 a,
struct bj_vec2 b )
inlinestatic

Component-wise minimum: (min(a.x,b.x), min(a.y,b.y)).

Parameters
aFirst vector.
bSecond vector.
Returns
Component-wise minimum.
See also
bj_vec2_max

Definition at line 322 of file vec.h.

Referenced by bj_vec2_min().

◆ bj_vec2_mul_comp()

struct bj_vec2 bj_vec2_mul_comp ( const struct bj_vec2 v,
const struct bj_vec2 s )
inlinestatic

Component-wise (Hadamard) product: (v.x*s.x, v.y*s.y).

Use when each axis needs its own factor: anisotropic scaling, per-axis modulation, blending channel weights.

Parameters
vInput vector.
sPer-component scale vector.
Returns
Component-wise product.
See also
bj_vec2_scale, bj_vec2_dot

Definition at line 175 of file vec.h.

Referenced by bj_vec2_mul_comp().

◆ bj_vec2_normalize()

struct bj_vec2 bj_vec2_normalize ( struct bj_vec2 v)
inlinestatic

Normalise to unit length (safe).

Computes v / ||v|| using one square root. Returns {0, 0} if the input is zero-length according to bj_real_is_zero, so it's safe on arbitrary input.

Parameters
vInput vector.
Returns
Unit-length copy of v, or {0, 0} if ||v|| == 0.
Warning
Zero detection follows bj_real_is_zero semantics and tolerance.
See also
bj_vec2_normalize_unsafe, bj_real_is_zero

Definition at line 286 of file vec.h.

Referenced by bj_mat4_rotate_arcball(), and bj_vec2_normalize().

◆ bj_vec2_normalize_unsafe()

struct bj_vec2 bj_vec2_normalize_unsafe ( struct bj_vec2 v)
inlinestatic

Normalise to unit length (unsafe).

Same as bj_vec2_normalize but skips the zero-length check. Faster on hot paths where the caller can guarantee non-zero length; passing a zero vector produces undefined results.

Parameters
vInput vector (must satisfy ||v|| > 0).
Returns
Unit-length copy of v.
Precondition
||v|| > 0 (caller guarantees).
Warning
Undefined if ||v|| == 0. Use bj_vec2_normalize when in doubt.
See also
bj_vec2_normalize

Definition at line 310 of file vec.h.

Referenced by bj_vec2_normalize_unsafe().

◆ bj_vec2_perp_dot()

bj_real bj_vec2_perp_dot ( struct bj_vec2 a,
struct bj_vec2 b )
inlinestatic

2D perp-dot product (signed scalar): a.x*b.y - a.y*b.x.

The 2D analogue of the 3D cross product's z component. Sign tells you the orientation of (a, b): positive if b is to the left of a, negative if to the right, zero if collinear. Magnitude equals twice the signed area of the triangle (0, a, b).

Useful for orientation tests, signed-area computations, and segment-intersection predicates.

Parameters
aFirst vector.
bSecond vector.
Returns
Signed scalar.
See also
bj_vec2_dot

Definition at line 209 of file vec.h.

◆ bj_vec2_scale()

struct bj_vec2 bj_vec2_scale ( struct bj_vec2 v,
bj_real s )
inlinestatic

Uniform scaling by a scalar: v * s.

Parameters
vInput vector.
sScalar factor.
Returns
Scaled vector.
See also
bj_vec2_mul_comp, bj_vec2_add_scaled
Examples
shaders.c.

Definition at line 160 of file vec.h.

Referenced by bj_vec2_scale(), bj_vec2_scale_to_len(), and shader_code().

◆ bj_vec2_scale_to_len()

struct bj_vec2 bj_vec2_scale_to_len ( struct bj_vec2 v,
bj_real L )
inlinestatic

Rescale a vector to a chosen length.

Equivalent to normalize(v) * L. Returns the zero vector if v is zero-length according to bj_real_is_zero, so it's safe on arbitrary input.

Parameters
vInput vector.
LDesired length of the result.
Returns
Rescaled vector, or the zero vector if v is zero.
See also
bj_vec2_normalize

Definition at line 235 of file vec.h.

Referenced by bj_vec2_scale_to_len().

◆ bj_vec2_sub()

struct bj_vec2 bj_vec2_sub ( const struct bj_vec2 lhs,
const struct bj_vec2 rhs )
inlinestatic

Component-wise subtraction: lhs - rhs.

Parameters
lhsLeft-hand vector.
rhsRight-hand vector.
Returns
Difference.
See also
bj_vec2_add, bj_vec2_distance
Examples
shaders.c.

Definition at line 146 of file vec.h.

Referenced by bj_vec2_sub(), and shader_code().

◆ bj_vec3_add()

struct bj_vec3 bj_vec3_add ( struct bj_vec3 lhs,
struct bj_vec3 rhs )
inlinestatic

Component-wise addition: lhs + rhs.

Parameters
lhsLeft-hand vector.
rhsRight-hand vector.
Returns
Sum.
See also
bj_vec3_sub, bj_vec3_add_scaled
Examples
shaders.c.

Definition at line 358 of file vec.h.

Referenced by bj_vec3_add(), and shader_code().

◆ bj_vec3_add_scaled()

struct bj_vec3 bj_vec3_add_scaled ( struct bj_vec3 lhs,
struct bj_vec3 rhs,
bj_real s )
inlinestatic

Fused scaled add: lhs + s * rhs.

Parameters
lhsBase vector.
rhsVector to add (after scaling).
sScalar weight applied to rhs.
Returns
lhs + s * rhs.
See also
bj_vec3_add, bj_vec3_scale

Definition at line 374 of file vec.h.

Referenced by bj_vec3_add_scaled().

◆ bj_vec3_cross()

struct bj_vec3 bj_vec3_cross ( struct bj_vec3 l,
struct bj_vec3 r )
inlinestatic

3D cross product: l × r (right-hand rule).

The result is perpendicular to both inputs; its length equals ||l|| * ||r|| * sin(theta) where theta is the angle between them. With the right-hand rule, x × y = z.

Parameters
lLeft-hand vector.
rRight-hand vector.
Returns
Cross product.
See also
bj_vec3_dot, bj_vec2_perp_dot

Definition at line 567 of file vec.h.

Referenced by bj_mat4_rotate_arcball(), bj_mat4_set_lookat(), bj_quat_rotate_vec3(), and bj_vec3_cross().

◆ bj_vec3_distance()

bj_real bj_vec3_distance ( struct bj_vec3 a,
struct bj_vec3 b )
inlinestatic

Euclidean distance: ||a - b||.

Parameters
aFirst vector.
bSecond vector.
Returns
Distance.
See also
bj_vec3_distance_sq

Definition at line 485 of file vec.h.

◆ bj_vec3_distance_sq()

bj_real bj_vec3_distance_sq ( struct bj_vec3 a,
struct bj_vec3 b )
inlinestatic

Squared Euclidean distance: ||a - b||^2.

Faster than bj_vec3_distance. No square root. Prefer this when you only need to compare distances or test against a squared threshold.

Parameters
aFirst vector.
bSecond vector.
Returns
Squared distance.
See also
bj_vec3_distance

Definition at line 471 of file vec.h.

Referenced by bj_vec3_distance().

◆ bj_vec3_dot()

bj_real bj_vec3_dot ( struct bj_vec3 a,
struct bj_vec3 b )
inlinestatic

Dot product: a.x*b.x + a.y*b.y + a.z*b.z.

Parameters
aFirst vector.
bSecond vector.
Returns
Scalar dot product.
See also
bj_vec3_cross, bj_vec3_len

Definition at line 426 of file vec.h.

Referenced by bj_mat4_orthonormalize(), bj_mat4_rotate_arcball(), and bj_vec3_reflect().

◆ bj_vec3_len()

bj_real bj_vec3_len ( struct bj_vec3 v)
inlinestatic

Euclidean length: sqrt(x^2 + y^2 + z^2).

Parameters
vInput vector.
Returns
Length.
See also
bj_vec3_distance, bj_vec3_normalize

Definition at line 436 of file vec.h.

Referenced by bj_mat4_rotate_axis_angle(), and bj_vec3_scale_to_len().

◆ bj_vec3_map()

struct bj_vec3 bj_vec3_map ( struct bj_vec3 a,
bj_real(* )(bj_real) )
inlinestatic

Apply a scalar function component-wise: (f(x), f(y), f(z)).

Parameters
aInput vector.
fFunction to apply to each component.
Returns
Vector with f applied to each component.
See also
bj_vec2_map, bj_vec4_map

Definition at line 344 of file vec.h.

Referenced by bj_vec3_map().

◆ bj_vec3_max()

struct bj_vec3 bj_vec3_max ( struct bj_vec3 a,
struct bj_vec3 b )
inlinestatic

Component-wise maximum.

Parameters
aFirst vector.
bSecond vector.
Returns
Component-wise maximum.
See also
bj_vec3_min

Definition at line 551 of file vec.h.

Referenced by bj_vec3_max().

◆ bj_vec3_min()

struct bj_vec3 bj_vec3_min ( struct bj_vec3 a,
struct bj_vec3 b )
inlinestatic

Component-wise minimum.

Parameters
aFirst vector.
bSecond vector.
Returns
Component-wise minimum.
See also
bj_vec3_max

Definition at line 536 of file vec.h.

Referenced by bj_vec3_min().

◆ bj_vec3_normalize()

struct bj_vec3 bj_vec3_normalize ( struct bj_vec3 v)
inlinestatic

Normalise to unit length (safe).

Computes v / ||v|| using one square root. Returns {0, 0, 0} if the input is zero-length according to bj_real_is_zero.

Parameters
vInput vector.
Returns
Unit-length copy of v, or {0, 0, 0} if ||v|| == 0.
Warning
Zero detection follows bj_real_is_zero semantics and tolerance.
See also
bj_vec3_normalize_unsafe, bj_real_is_zero

Definition at line 502 of file vec.h.

Referenced by bj_mat4_orthonormalize(), bj_mat4_set_lookat(), and bj_vec3_normalize().

◆ bj_vec3_normalize_unsafe()

struct bj_vec3 bj_vec3_normalize_unsafe ( struct bj_vec3 v)
inlinestatic

Normalise to unit length (unsafe).

Same as bj_vec3_normalize but skips the zero-length check.

Parameters
vInput vector (must satisfy ||v|| > 0).
Returns
Unit-length copy of v.
Precondition
||v|| > 0 (caller guarantees).
Warning
Undefined if ||v|| == 0. Use bj_vec3_normalize when in doubt.
See also
bj_vec3_normalize

Definition at line 524 of file vec.h.

Referenced by bj_vec3_normalize_unsafe().

◆ bj_vec3_reflect()

struct bj_vec3 bj_vec3_reflect ( struct bj_vec3 v,
struct bj_vec3 n )
inlinestatic

Reflect a vector about a surface normal: v - 2*dot(v, n)*n.

Mirrors v across the plane defined by the normal n. The result is only a true reflection when n is unit length; caller is responsible for normalising if needed.

Parameters
vInput vector.
nSurface normal (expected to be unit length).
Returns
Reflected vector.
See also
bj_vec3_normalize

Definition at line 588 of file vec.h.

Referenced by bj_vec3_reflect().

◆ bj_vec3_scale()

struct bj_vec3 bj_vec3_scale ( struct bj_vec3 v,
bj_real s )
inlinestatic

Uniform scaling by a scalar: v * s.

Parameters
vInput vector.
sScalar factor.
Returns
Scaled vector.
See also
bj_vec3_add_scaled
Examples
shaders.c.

Definition at line 411 of file vec.h.

Referenced by bj_mat4_orthonormalize(), bj_vec3_scale(), bj_vec3_scale_to_len(), and shader_code().

◆ bj_vec3_scale_to_len()

struct bj_vec3 bj_vec3_scale_to_len ( struct bj_vec3 v,
bj_real L )
inlinestatic

Rescale a vector to a chosen length.

Equivalent to normalize(v) * L. Returns the zero vector if v is zero-length according to bj_real_is_zero.

Parameters
vInput vector.
LDesired length of the result.
Returns
Rescaled vector, or the zero vector if v is zero.
See also
bj_vec3_normalize

Definition at line 451 of file vec.h.

Referenced by bj_vec3_scale_to_len().

◆ bj_vec3_sub()

struct bj_vec3 bj_vec3_sub ( struct bj_vec3 lhs,
struct bj_vec3 rhs )
inlinestatic

Component-wise subtraction: lhs - rhs.

Parameters
lhsLeft-hand vector.
rhsRight-hand vector.
Returns
Difference.
See also
bj_vec3_add, bj_vec3_distance

Definition at line 393 of file vec.h.

Referenced by bj_mat4_orthonormalize(), bj_mat4_set_lookat(), and bj_vec3_sub().

◆ bj_vec4_add()

struct bj_vec4 bj_vec4_add ( struct bj_vec4 lhs,
struct bj_vec4 rhs )
inlinestatic

Component-wise addition: lhs + rhs.

Parameters
lhsLeft-hand vector.
rhsRight-hand vector.
Returns
Sum.
See also
bj_vec4_sub, bj_vec4_add_scaled

Definition at line 619 of file vec.h.

Referenced by bj_vec4_add().

◆ bj_vec4_add_scaled()

struct bj_vec4 bj_vec4_add_scaled ( struct bj_vec4 lhs,
struct bj_vec4 rhs,
bj_real s )
inlinestatic

Fused scaled add: lhs + s * rhs.

Parameters
lhsBase vector.
rhsVector to add (after scaling).
sScalar weight applied to rhs.
Returns
lhs + s * rhs.
See also
bj_vec4_add, bj_vec4_scale

Definition at line 636 of file vec.h.

Referenced by bj_vec4_add_scaled().

◆ bj_vec4_cross_xyz()

struct bj_vec4 bj_vec4_cross_xyz ( struct bj_vec4 l,
struct bj_vec4 r )
inlinestatic

Cross product of the XYZ components, with w = 0.

Computes the 3D cross product on the xyz part of two 4D vectors, ignoring w. The result's w is set to zero so the output behaves as a direction (not a point) under homogeneous transforms.

Parameters
lLeft-hand vector.
rRight-hand vector.
Returns
Cross product with w = 0.
See also
bj_vec3_cross

Definition at line 789 of file vec.h.

Referenced by bj_vec4_cross_xyz().

◆ bj_vec4_dot()

bj_real bj_vec4_dot ( struct bj_vec4 a,
struct bj_vec4 b )
inlinestatic

Dot product over all four components.

Parameters
aFirst vector.
bSecond vector.
Returns
Scalar dot product.
See also
bj_vec4_len, bj_quat_dot

Definition at line 691 of file vec.h.

Referenced by bj_mat4_translate(), and bj_vec4_reflect().

◆ bj_vec4_len()

bj_real bj_vec4_len ( struct bj_vec4 v)
inlinestatic

Euclidean length: sqrt(x^2 + y^2 + z^2 + w^2).

Parameters
vInput vector.
Returns
Length.
See also
bj_vec4_normalize

Definition at line 701 of file vec.h.

◆ bj_vec4_map()

struct bj_vec4 bj_vec4_map ( struct bj_vec4 a,
bj_real(* )(bj_real) )
inlinestatic

Apply a scalar function component-wise to all four components.

Parameters
aInput vector.
fFunction to apply to each component.
Returns
Vector with f applied to each component.
See also
bj_vec2_map, bj_vec3_map

Definition at line 605 of file vec.h.

Referenced by bj_vec4_map().

◆ bj_vec4_max()

struct bj_vec4 bj_vec4_max ( struct bj_vec4 a,
struct bj_vec4 b )
inlinestatic

Component-wise maximum across all four components.

Parameters
aFirst vector.
bSecond vector.
Returns
Component-wise maximum.
See also
bj_vec4_min

Definition at line 768 of file vec.h.

Referenced by bj_vec4_max().

◆ bj_vec4_min()

struct bj_vec4 bj_vec4_min ( struct bj_vec4 a,
struct bj_vec4 b )
inlinestatic

Component-wise minimum across all four components.

Parameters
aFirst vector.
bSecond vector.
Returns
Component-wise minimum.
See also
bj_vec4_max

Definition at line 752 of file vec.h.

Referenced by bj_vec4_min().

◆ bj_vec4_normalize()

struct bj_vec4 bj_vec4_normalize ( struct bj_vec4 v)
inlinestatic

Normalise to unit length (safe).

Computes v / ||v|| using one square root. Returns {0, 0, 0, 0} if the input is zero-length according to bj_real_is_zero.

Parameters
vInput vector.
Returns
Unit-length copy of v, or {0, 0, 0, 0} if ||v|| == 0.
Warning
Zero detection follows bj_real_is_zero semantics and tolerance.
See also
bj_vec4_normalize_unsafe, bj_real_is_zero

Definition at line 718 of file vec.h.

Referenced by bj_vec4_normalize().

◆ bj_vec4_normalize_unsafe()

struct bj_vec4 bj_vec4_normalize_unsafe ( struct bj_vec4 v)
inlinestatic

Normalise to unit length (unsafe).

Same as bj_vec4_normalize but skips the zero-length check.

Parameters
vInput vector (must satisfy ||v|| > 0).
Returns
Unit-length copy of v.
Precondition
||v|| > 0 (caller guarantees).
Warning
Undefined if ||v|| == 0. Use bj_vec4_normalize when in doubt.
See also
bj_vec4_normalize

Definition at line 740 of file vec.h.

Referenced by bj_vec4_normalize_unsafe().

◆ bj_vec4_reflect()

struct bj_vec4 bj_vec4_reflect ( struct bj_vec4 v,
struct bj_vec4 n )
inlinestatic

Reflect a vector about a surface normal: v - 2*dot(v, n)*n.

Mirrors v across the plane defined by the normal n. The result is only a true reflection when n is unit length.

Parameters
vInput vector.
nSurface normal (expected to be unit length).
Returns
Reflected vector.
See also
bj_vec3_reflect, bj_vec4_normalize

Definition at line 809 of file vec.h.

Referenced by bj_vec4_reflect().

◆ bj_vec4_scale()

struct bj_vec4 bj_vec4_scale ( struct bj_vec4 v,
bj_real s )
inlinestatic

Uniform scaling by a scalar: v * s.

Parameters
vInput vector.
sScalar factor.
Returns
Scaled vector.
See also
bj_vec4_add_scaled

Definition at line 675 of file vec.h.

Referenced by bj_vec4_scale().

◆ bj_vec4_sub()

struct bj_vec4 bj_vec4_sub ( struct bj_vec4 lhs,
struct bj_vec4 rhs )
inlinestatic

Component-wise subtraction: lhs - rhs.

Parameters
lhsLeft-hand vector.
rhsRight-hand vector.
Returns
Difference.
See also
bj_vec4_add

Definition at line 656 of file vec.h.

Referenced by bj_vec4_sub().