]> git.itanic.dy.fi Git - sdl-planets/blob - vector.h
quadtree: Add a tree validator
[sdl-planets] / vector.h
1 #ifndef _VECTOR_H
2 #define _VECTOR_H
3
4 #include <math.h>
5
6 struct vector {
7         double x;
8         double y;
9 };
10
11 /*
12  * res = a - b
13  */
14 static inline void vector_sub(const struct vector *a, const struct vector *b,
15                 struct vector *res)
16 {
17         res->x = a->x - b->x;
18         res->y = a->y - b->y;
19 }
20
21 /*
22  * res = a + b
23  */
24 static inline void vector_add(const struct vector *a, const struct vector *b,
25                 struct vector *res)
26 {
27         res->x = a->x + b->x;
28         res->y = a->y + b->y;
29 }
30
31 static inline double vector_abs(const struct vector *a)
32 {
33         return sqrt(a->x * a->x + a->y * a->y);
34 }
35
36 static inline void vector_scale(const struct vector *a, const double b,
37                                 struct vector *res)
38 {
39         res->x = a->x * b;
40         res->y = a->y * b;
41 }
42
43 static inline void vector_div(const struct vector *a, const double b,
44                               struct vector *res)
45 {
46         res->x = a->x / b;
47         res->y = a->y / b;
48 }
49
50 #endif