]> git.lizzy.rs Git - nothing.git/blob - src/math/vec.h
Include math in vec.h
[nothing.git] / src / math / vec.h
1 #ifndef POINT_H_
2 #define POINT_H_
3
4 #include <math.h>
5 #include "math/pi.h"
6
7 typedef struct {
8     float x, y;
9 } Vec2f;
10
11 static inline
12 Vec2f vec(float x, float y)
13 {
14     Vec2f result = {
15         .x = x,
16         .y = y
17     };
18     return result;
19 }
20
21 static inline
22 Vec2f vec_scala_mult(Vec2f v, float scalar)
23 {
24     Vec2f result = {
25         .x = v.x * scalar,
26         .y = v.y * scalar
27     };
28     return result;
29 }
30
31 static inline
32 Vec2f vec_from_polar(float arg, float mag)
33 {
34     return vec_scala_mult(
35         vec(cosf(arg), sinf(arg)),
36         mag);
37 }
38
39 static inline
40 Vec2f vec_from_ps(Vec2f p1, Vec2f p2)
41 {
42     Vec2f result = {
43         .x = p2.x - p1.x,
44         .y = p2.y - p1.y
45     };
46     return result;
47 }
48
49 static inline
50 float vec_arg(Vec2f v)
51 {
52     return atan2f(v.y, v.x);
53 }
54
55 static inline
56 float vec_mag(Vec2f v)
57 {
58     return sqrtf(v.x * v.x + v.y * v.y);
59 }
60
61 static inline
62 Vec2f vec_sum(Vec2f v1, Vec2f v2)
63 {
64     Vec2f result = {
65         .x = v1.x + v2.x,
66         .y = v1.y + v2.y
67     };
68     return result;
69 }
70
71 static inline
72 Vec2f vec_sub(Vec2f v1, Vec2f v2)
73 {
74     Vec2f result = {
75         .x = v1.x - v2.x,
76         .y = v1.y - v2.y
77     };
78     return result;
79 }
80
81 static inline
82 Vec2f vec_neg(Vec2f v)
83 {
84     Vec2f result = {
85         .x = -v.x,
86         .y = -v.y
87     };
88
89     return result;
90 }
91
92 static inline
93 float vec_length(Vec2f v)
94 {
95     return sqrtf(v.x * v.x + v.y * v.y);
96 }
97
98 static inline
99 void vec_add(Vec2f *v1, Vec2f v2)
100 {
101     v1->x += v2.x;
102     v1->y += v2.y;
103 }
104
105 static inline
106 Vec2f vec_entry_mult(Vec2f v1, Vec2f v2)
107 {
108     Vec2f result = {
109         .x = v1.x * v2.x,
110         .y = v1.y * v2.y
111     };
112
113     return result;
114 }
115
116 static inline
117 Vec2f vec_entry_div(Vec2f v1, Vec2f v2)
118 {
119     Vec2f result = {
120         .x = v1.x / v2.x,
121         .y = v1.y / v2.y
122     };
123
124     return result;
125 }
126
127 static inline
128 float rad_to_deg(float a)
129 {
130     return 180 / PI * a;
131 }
132
133 static inline
134 Vec2f vec_norm(Vec2f v)
135 {
136     // TODO(#657): math/point/vec_norm: using vec_length is too expensive
137     //   It involves multiplication and sqrt. We can just check if its components are close to 0.0f.
138
139     const float l = vec_length(v);
140
141     if (l < 1e-6) {
142         return vec(0.0f, 0.0f);
143     }
144
145     return vec(v.x / l, v.y / l);
146 }
147
148 static inline
149 float vec_sqr_norm(Vec2f v)
150 {
151     return v.x * v.x + v.y * v.y;
152 }
153
154 #define vec_scale vec_scala_mult
155
156 typedef struct {
157     int x, y;
158 } Vec2i;
159
160 static inline
161 Vec2i vec2i(int x, int y)
162 {
163     Vec2i resoolt = {
164         .x = x,
165         .y = y
166     };
167     return resoolt;
168 }
169
170 #endif  // POINT_H_