From 53f68bb7b0dce309723675c4b97f726a469031c0 Mon Sep 17 00:00:00 2001 From: Joel Stålnacke Date: Sun, 13 Oct 2024 15:09:24 +0300 Subject: Added more math functions and sanity checks No +/-Inf or NaN in my vectors please :) --- lib/vec2.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib/vec2.c') diff --git a/lib/vec2.c b/lib/vec2.c index b417e9c..d795d50 100644 --- a/lib/vec2.c +++ b/lib/vec2.c @@ -1,9 +1,16 @@ +#include +#include + #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); +} -- cgit v1.2.3