aboutsummaryrefslogtreecommitdiff
path: root/src/vec2.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:28:13 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:47:34 +0000
commitcb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch)
treec7d90c0abe9533c87d51b5196871846855efa1ba /src/vec2.c
downloadthesis-cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f.tar.gz
publishHEADmaster
Diffstat (limited to 'src/vec2.c')
-rw-r--r--src/vec2.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/vec2.c b/src/vec2.c
new file mode 100644
index 0000000..7c325fe
--- /dev/null
+++ b/src/vec2.c
@@ -0,0 +1,67 @@
1#include "vec2.h"
2#include <math.h>
3
4struct vec2
5vec2_add (struct vec2 a, struct vec2 b)
6{
7 return (struct vec2) { a.x + b.x, a.y + b.y };
8}
9
10struct vec2
11vec2_sub (struct vec2 a, struct vec2 b)
12{
13 return (struct vec2) { a.x - b.x, a.y - b.y };
14}
15
16struct vec2
17vec2_scale (struct vec2 a, double k)
18{
19 return (struct vec2) { a.x*k, a.y*k };
20}
21
22double
23vec2_dot (struct vec2 a, struct vec2 b)
24{
25 return a.x*b.x + a.y*b.y;
26}
27
28double
29vec2_norm (struct vec2 a)
30{
31 return hypot (a.x, a.y);
32}
33
34double
35vec2_dist (struct vec2 a, struct vec2 b)
36{
37 return vec2_norm (vec2_sub (a, b));
38}
39
40struct vec2
41vec2_unit (struct vec2 a)
42{
43 double n = vec2_norm (a);
44
45 // if (n <= VEC2_EPS)
46 // return (struct vec2) { 0, 0 };
47
48 return vec2_scale (a, 1/n);
49}
50
51double
52vec2_angle0 (struct vec2 a)
53{
54 return atan2 (a.y, a.x);
55}
56
57static double
58clampd (double x, double lo, double hi)
59{
60 return x < lo ? lo : (x > hi ? hi : x);
61}
62
63double
64vec2_angle (struct vec2 a, struct vec2 b)
65{
66 return acos (clampd (vec2_dot (a, b)/(vec2_norm (a)*vec2_norm (b)), 0, 1));
67}