-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.h
96 lines (76 loc) · 1.63 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <glib.h>
#include <math.h>
typedef struct {
gdouble x;
gdouble y;
} Vector;
#define vector_is_null(v) ((v)->x == 0.0 && (v)->y == 0.0)
#define vector_init(v) ((v)->x = (v)->y = 0)
static inline void vector_set(Vector *v, gdouble x, gdouble y)
{
v->x = x;
v->y = y;
}
static inline void vector_add(Vector *v1, Vector *v2)
{
v1->x += v2->x;
v1->y += v2->y;
}
static inline void vector_sub(Vector *v1, Vector *v2)
{
v1->x -= v2->x;
v1->y -= v2->y;
}
static inline void vector_div(Vector *v, gdouble scalar)
{
if (!scalar)
return;
v->x /= scalar;
v->y /= scalar;
}
static inline void vector_mult(Vector *v, gdouble scalar)
{
v->x *= scalar;
v->y *= scalar;
}
static inline void vector_mult2(Vector *v, gdouble scalar, Vector *res)
{
*res = *v;
vector_mult(res, scalar);
}
static inline gdouble vector_mag(Vector *v)
{
return sqrt(v->x * v->x + v->y * v->y);
}
static inline gdouble vector_dot(Vector *v1, Vector *v2)
{
return ((v1->x * v2->x) + (v1->y * v2->y));
}
/*
* Calculate cos(a) where 'a' is the angle between the 2 vectors v1 and v2
* cos(a) = v1.v2 / |v1|.|v2|
*/
static inline gdouble vector_cos_angle(Vector *v1, Vector *v2)
{
return (vector_dot(v1, v2) / (vector_mag(v1) * vector_mag(v2)));
}
static inline void vector_normalize(Vector *v)
{
gdouble mag;
mag = vector_mag(v);
vector_div(v, mag);
}
static inline void vector_set_mag(Vector *v, gdouble mag)
{
vector_normalize(v);
vector_mult(v, mag);
}
static inline void vector_print(Vector *v, char *name)
{
if (!name)
name = "Vector";
g_printf("%s (%g, %g)\n", name, v->x, v->y);
}
#endif /* __VECTOR_H__ */