summaryrefslogtreecommitdiff
path: root/src/vector.c
diff options
context:
space:
mode:
authorAmin Mesbah <mesbahamin@gmail.com>2017-12-18 00:26:53 -0800
committerAmin Mesbah <mesbahamin@gmail.com>2017-12-18 00:26:53 -0800
commit94fca2bb4d579ac4dbb51dc57ee395b29c21a887 (patch)
tree6f517ae95c50e080c77bd365ac1d187472f9bb28 /src/vector.c
parentb372c824cbe993191425a7315350bde81ba3e67b (diff)
downloadohsp-94fca2bb4d579ac4dbb51dc57ee395b29c21a887.zip
ohsp-94fca2bb4d579ac4dbb51dc57ee395b29c21a887.tar.gz
Move vector stuff to it's own files
Diffstat (limited to 'src/vector.c')
-rw-r--r--src/vector.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/vector.c b/src/vector.c
new file mode 100644
index 0000000..baef5fc
--- /dev/null
+++ b/src/vector.c
@@ -0,0 +1,68 @@
+#include "vector.h"
+
+#include <math.h>
+
+
+struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1)
+{
+ return vec2d_add_c(v0.x, v0.y, v1.x, v1.y);
+}
+
+
+struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1)
+{
+ struct Vec2d new_vec =
+ {
+ .x = x0 + x1,
+ .y = y0 + y1
+ };
+ return new_vec;
+}
+
+
+bool vec2d_equal(struct Vec2d v0, struct Vec2d v1)
+{
+ return (v0.x == v1.x) && (v0.y == v1.y);
+}
+
+
+float vec2d_get_angle(float x, float y)
+{
+ return 0.5f * M_PI - atan2f(y, x);
+}
+
+
+float vec2d_get_length(float x, float y)
+{
+ return hypotf(x, y);
+}
+
+
+struct Vec2d vec2d_negate(struct Vec2d v)
+{
+ return vec2d_scale_c(v.x, v.y, -1.0f);
+}
+
+
+struct Vec2d vec2d_normalize(float x, float y)
+{
+ float length = vec2d_get_length(x, y);
+ return vec2d_scale_c(x, y, 1.0f / length);
+}
+
+
+struct Vec2d vec2d_scale(struct Vec2d v, float s)
+{
+ return vec2d_scale_c(v.x, v.y, s);
+}
+
+
+struct Vec2d vec2d_scale_c(float x, float y, float s)
+{
+ struct Vec2d new_vec =
+ {
+ .x = x * s,
+ .y = y * s
+ };
+ return new_vec;
+}