diff options
author | rodri <rgl@antares-labs.eu> | 2023-01-28 22:16:25 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-01-28 22:16:25 +0000 |
commit | 983fae140a1e162d947eab8b8a0d9acba291d3a8 (patch) | |
tree | ef7a0de9a51ebbfd086e9293d2a22e1da387ee2d /vector.c | |
download | threedee-front.tar.gz threedee-front.tar.bz2 threedee-front.zip |
Diffstat (limited to 'vector.c')
-rw-r--r-- | vector.c | 60 |
1 files changed, 60 insertions, 0 deletions
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 <u.h> +#include <libc.h> +#include <draw.h> +#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; +} |