f36724c40601410f8c32608f50cd4c9dad2c2707
[aversive.git] / modules / base / math / geometry / vect_base.c
1 #include <stdint.h>
2 #include <math.h>
3 #include <vect_base.h>
4  
5 /* Return scalar product */
6 int32_t 
7 vect_pscal(vect_t *v, vect_t *w)
8 {
9         return v->x * w->x + v->y * w->y;
10 }
11
12 /* Return Z of vectorial product */
13 int32_t 
14 vect_pvect(vect_t *v, vect_t *w)
15 {
16         return v->x*w->y - v->y*w->x;
17 }
18
19 /* Return scalar product */
20 int8_t 
21 vect_pscal_sign(vect_t *v, vect_t *w)
22 {
23         int32_t z;
24         z = vect_pscal(v, w);
25         if (z==0)
26                 return 0;
27         return z>0?1:-1;
28 }
29
30 /* Return Z of vectorial product */
31 int8_t 
32 vect_pvect_sign(vect_t *v, vect_t *w)
33 {
34         int32_t z;
35         z = vect_pvect(v, w);
36         if (z==0)
37                 return 0;
38         return z>0?1:-1;
39 }
40
41 /* norm of a vector */
42 float
43 vect_norm(vect_t *v)
44 {
45         return sqrt(v->x*v->x+v->y*v->y);
46 }
47
48
49
50 void vect_rot_trigo(vect_t *v)
51 {
52         int32_t s;
53     
54         s = v->x;
55         v->x= -v->y;
56         v->y = s;
57 }    
58
59 void vect_rot_retro(vect_t *v)
60 {
61         int32_t s;
62     
63         s = v->x;
64         v->x= v->y;
65         v->y = -s;
66 }    
67
68
69 float vect_get_angle(vect_t *v, vect_t *w)
70 {
71         int32_t ps;
72         float n;
73         
74         ps = vect_pscal(v, w);
75         n = vect_norm(v) * vect_norm(w);
76         
77         return acos((float)ps/n);
78 }