From 983fae140a1e162d947eab8b8a0d9acba291d3a8 Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 28 Jan 2023 22:16:25 +0000 Subject: initial public release. --- vector.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 vector.c (limited to 'vector.c') diff --git a/vector.c b/vector.c new file mode 100644 index 0000000..3595c1f --- /dev/null +++ b/vector.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include "dat.h" +#include "fns.h" + +Vector +Vec(double x, double y) +{ + return (Vector){x, y}; +} + +Vector +addvec(Vector v, Vector u) +{ + return (Vector){v.x+u.x, v.y+u.y}; +} + +Vector +subvec(Vector v, Vector u) +{ + return (Vector){v.x-u.x, v.y-u.y}; +} + +Vector +mulvec(Vector v, double s) +{ + return (Vector){v.x*s, v.y*s}; +} + +double +dotvec(Vector v, Vector u) +{ + return v.x*u.x + v.y*u.y; +} + +Vector +normvec(Vector v) +{ + double len; + + len = hypot(v.x, v.y); + if(len == 0) + return (Vector){0, 0}; + v.x /= len; + v.y /= len; + return v; +} + +Vector +rotatevec(Vector p, double θ, Vector c) +{ + Vector r; + + p = subvec(p, c); + r.x = p.x*cos(θ) - p.y*sin(θ); + r.y = p.x*sin(θ) + p.y*cos(θ); + r = addvec(r, c); + return r; +} -- cgit v1.2.3