diff options
| author | Joel Stålnacke <joel@saker.fi> | 2024-10-13 15:09:24 +0300 |
|---|---|---|
| committer | Joel Stålnacke <joel@saker.fi> | 2024-10-13 15:09:24 +0300 |
| commit | 53f68bb7b0dce309723675c4b97f726a469031c0 (patch) | |
| tree | bb156bf099abd5b054822bb7a0cc59cd1ec036c7 /lib/vec2.c | |
| parent | 6a9b5df8b5ed811fbfde9c78f2599247751a650f (diff) | |
Added more math functions and sanity checks
No +/-Inf or NaN in my vectors please :)
Diffstat (limited to 'lib/vec2.c')
| -rw-r--r-- | lib/vec2.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -1,9 +1,16 @@ +#include <assert.h> +#include <math.h> + #include "types.h" vec2 dp_vec2_new(float x, float y) { vec2 new; + + assert(isfinite(x)); + assert(isfinite(y)); + new.x = x; new.y = y; return new; @@ -44,3 +51,17 @@ dp_vec2_dot(vec2 a, vec2 b) dot.y = a.y * b.y; return dot; } + +float +dp_vec2_length(vec2 v) +{ + return sqrtf(powf(v.x, 2.f) + powf(v.y, 2.f)); +} + +vec2 +dp_vec2_normal(vec2 v) +{ + float a; + a = 1.f / dp_vec2_length(v); + return dp_vec2_mul(v, a); +} |
