Skip to content

mar4elkin/jaml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JAML - just another math lib

Educational project: a tiny header-only 2D vector math helper (vector2.h) and a minimal Win32 GDI viewer (viewer_win32.c) to visualize vectors, coordinate axes, and small demos (presets).

Examples

Base vectors

vec2 a = (vec2){ 3.0f, 4.0f };
vec2 b = (vec2){ -1.5f, 2.0f };

add / sub / mul

vec2 add = vec2_add(&a, &b);                // (4.500, 6.000)
vec2 sub = vec2_sub(&a, &b);                // (4.500, 2.000)
vec2 mul = vec2_mul(&a, 2.0f);              // (6.000, 8.000)

length2 / length

vec2_length2(&a); // 25.000
vec2_length(&a);  // 5.000

dist2 / dist

vec2_dist2(&a, &b); // 24.250
vec2_dist(&a, &b);  // ~4.924

normalize

vec2_normalize(&a); // (0.600, 0.800)

dot / cross

vec2_dot(&a, &b); // 3.500
vec2_cross(&a, &b); // 12.000

angle (radians / degrees)

float ang = vec2_angle(&a, &b);                 // ~1.287003 rad
float deg = ang * (180.0f / 3.14159265358979f); // ~73.740°

equal (tolerant compare with EPSILON)

vec2 a_close = (vec2){ a.x + 1e-7f, a.y };
vec2_equal(&a, &a, EPSILON);       // true
vec2_equal(&a, &a_close, EPSILON); // true

min / max (component-wise)

vec2 vmin = vec2_min(&a, &b); // (-1.500, 2.000)
vec2 vmax = vec2_max(&a, &b); // ( 3.000, 4.000)

abs (component-wise)

vec2 v  = (vec2){ -2.0f, 5.0f };
vec2 av = vec2_abs(&v); // (2.000, 5.000)

perp (90° CCW)

vec2 p = vec2_perp(&a); // (-4.000, 3.000)

project / reject

vec2 proj = vec2_project(&a, &b); // (-0.840, 1.120)
vec2 rej  = vec2_reject(&a, &b);  // ( 3.840, 2.880)

reflect (incident about normal)

vec2 i = (vec2){ 3.0f, -2.0f };
vec2 nrm = (vec2){ 0.0f, 1.0f };
vec2 r = vec2_reflect(&i, &nrm); // (3.000, 2.000)

rotate (around origin)

vec2 v0 = (vec2){ 2.0f, 0.0f };
float rad = 3.14159265358979f / 4.0f; // 45°
vec2 vr = vec2_rotate(&v0, rad);      // (~1.414, ~1.414)

rotate_around (about pivot)

vec2 pt    = (vec2){ 2.0f, 1.0f };
vec2 pivot = (vec2){ 1.0f, 0.0f };
float r90  = 3.14159265358979f / 2.0f; // 90°
vec2 out   = vec2_rotate_around(&pt, &pivot, r90); // (0.000, 1.000)

rot90 helpers

vec2 u = (vec2){ 5.0f, 2.0f };
vec2 u_ccw = vec2_rot90_ccw(&u); // (-2.000, 5.000)
vec2 u_cw  = vec2_rot90_cw(&u);  // ( 2.000,-5.000)

Visualizations

empty preset empty rotations preset rotations reflections preset reflections projection preset projection basics preset basics

Types & Constants

  • vec2 — { float x, y; }.
  • EPSILON — small positive constant used for tolerance in comparisons.

Basic Ops (component-wise)

  • vec2 vec2_add(vec2* a, vec2* b) → (a.x+b.x, a.y+b.y)
  • vec2 vec2_sub(vec2* a, vec2* b) → (a.x-b.x, a.y-b.y)
  • vec2 vec2_mul(vec2* a, float s) → (a.xs, a.ys)

Length & Distance

  • float vec2_length2(vec2* a) → x² + y² (no sqrt, fast)
  • float vec2_length(vec2* a) → sqrt(x² + y²)
  • float vec2_dist2(vec2* a, vec2* b) → (ax-bx)² + (ay-by)²
  • float vec2_dist(vec2* a, vec2* b) → sqrt(dist2)

Normalization

  • vec2 vec2_normalize(vec2* a) → unit vector a/|a|; returns (0,0) if |a|==0.

Products

  • float vec2_dot(vec2* a, vec2* b) → axbx + ayby
  • float vec2_cross(vec2* a, vec2* b) → axby - aybx

Angle

  • float vec2_angle(vec2* a, vec2* b)

Equality with Tolerance

  • bool vec2_equal(vec2* a, vec2* b, float eps)

Component-wise Helpers

  • vec2 vec2_min(vec2* a, vec2* b) → (min(ax,bx), min(ay,by))
  • vec2 vec2_max(vec2* a, vec2* b) → (max(ax,bx), max(ay,by))
  • vec2 vec2_abs(vec2* a) → (|ax|, |ay|)
  • vec2 vec2_perp(vec2* a) → 90° CCW perpendicular (-y, x)

Projection / Rejection / Reflection

  • vec2 vec2_project(vec2* a, vec2* b)
  • vec2 vec2_reject(vec2* a, vec2* b) → a - proj_b(a)
  • vec2 vec2_reflect(vec2* a, vec2* n)

Rotation

  • vec2 vec2_rotate(vec2* a, float radians)
  • vec2 vec2_rotate_around(const vec2* v, const vec2* pivot, float radians)
  • vec2 vec2_rot90_ccw(const vec2* v) → +90° (-y, x)
  • vec2 vec2_rot90_cw(const vec2* v) → −90° (y, -x)

About

A tiny header-only 2D vector math helper

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published