summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rwxr-xr-xlib/build.sh2
-rw-r--r--lib/types.h2
-rw-r--r--lib/vec2.c21
3 files changed, 24 insertions, 1 deletions
diff --git a/lib/build.sh b/lib/build.sh
index 725c237..f305939 100755
--- a/lib/build.sh
+++ b/lib/build.sh
@@ -7,7 +7,7 @@ fail() {
CC=cc
CFLAGS="-std=c99 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L"
-LDFLAGS="-fPIC"
+LDFLAGS="-fPIC -lm"
SOURCES="test.c vec2.c world.c log.c"
output="build"
diff --git a/lib/types.h b/lib/types.h
index 0619c8a..8618df9 100644
--- a/lib/types.h
+++ b/lib/types.h
@@ -8,3 +8,5 @@ vec2 dp_vec2_add(vec2, vec2);
vec2 dp_vec2_sub(vec2, vec2);
vec2 dp_vec2_mul(vec2, float scalar);
vec2 dp_vec2_dot(vec2, vec2);
+float dp_vec2_length(vec2);
+vec2 dp_vec2_normal(vec2);
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 <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);
+}