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).
vec2 a = (vec2){ 3.0f, 4.0f };
vec2 b = (vec2){ -1.5f, 2.0f };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)vec2_length2(&a); // 25.000
vec2_length(&a); // 5.000vec2_dist2(&a, &b); // 24.250
vec2_dist(&a, &b); // ~4.924vec2_normalize(&a); // (0.600, 0.800)vec2_dot(&a, &b); // 3.500
vec2_cross(&a, &b); // 12.000float ang = vec2_angle(&a, &b); // ~1.287003 rad
float deg = ang * (180.0f / 3.14159265358979f); // ~73.740°vec2 a_close = (vec2){ a.x + 1e-7f, a.y };
vec2_equal(&a, &a, EPSILON); // true
vec2_equal(&a, &a_close, EPSILON); // truevec2 vmin = vec2_min(&a, &b); // (-1.500, 2.000)
vec2 vmax = vec2_max(&a, &b); // ( 3.000, 4.000)vec2 v = (vec2){ -2.0f, 5.0f };
vec2 av = vec2_abs(&v); // (2.000, 5.000)vec2 p = vec2_perp(&a); // (-4.000, 3.000)vec2 proj = vec2_project(&a, &b); // (-0.840, 1.120)
vec2 rej = vec2_reject(&a, &b); // ( 3.840, 2.880)vec2 i = (vec2){ 3.0f, -2.0f };
vec2 nrm = (vec2){ 0.0f, 1.0f };
vec2 r = vec2_reflect(&i, &nrm); // (3.000, 2.000)vec2 v0 = (vec2){ 2.0f, 0.0f };
float rad = 3.14159265358979f / 4.0f; // 45°
vec2 vr = vec2_rotate(&v0, rad); // (~1.414, ~1.414)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)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)
preset empty
preset rotations
preset reflections
preset projection
preset basics
- vec2 — { float x, y; }.
- EPSILON — small positive constant used for tolerance in comparisons.
- 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)
- 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)
- vec2 vec2_normalize(vec2* a) → unit vector a/|a|; returns (0,0) if |a|==0.
- float vec2_dot(vec2* a, vec2* b) → axbx + ayby
- float vec2_cross(vec2* a, vec2* b) → axby - aybx
- float vec2_angle(vec2* a, vec2* b)
- bool vec2_equal(vec2* a, vec2* b, float eps)
- 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)
- 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)
- 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)