vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / modules / base / math / vect2 / vect2.c
1 #include "vect2.h"
2
3 #include <stdlib.h>
4 #include <math.h>
5
6 /* Convert a polar vector to a cartesian one */
7 void vect2_pol2cart(vect2_pol* vp, vect2_cart* vc)
8 {
9    if(vp == NULL) return;
10    if(vc == NULL) return;
11    
12    vc->x = (Real)( (vp->r)*cos(vp->theta) );
13    vc->y = (Real)( (vp->r)*sin(vp->theta) );
14    
15    return;
16 }
17
18 /* Convert a cartesian vector to a polar one */
19 void vect2_cart2pol(vect2_cart* vc, vect2_pol* vp)
20 {
21    if(vc == NULL) return;
22    if(vp == NULL) return;
23    
24    vp->r = (Real)( sqrt((vc->x)*(vc->x)+(vc->y)*(vc->y)) );
25    vp->theta = (Real)atan2(vc->y,vc->x);
26    
27    return;
28 }
29
30
31 /* Add 2 polar vectors and return the result */
32 void vect2_add_pol(vect2_pol* vp1, vect2_pol* vp2, vect2_pol* vresult)
33 {
34         vect2_cart vc1;
35         vect2_cart vc2;
36
37         vect2_cart vc;
38
39         vect2_pol2cart(vp1,&vc1);
40         vect2_pol2cart(vp2,&vc2);
41         
42         vect2_add_cart(&vc1,&vc2,&vc);
43         
44         vect2_cart2pol(&vc,vresult);
45         
46         return;
47 }
48
49 /* Add 2 cartesian vectors and return the result */
50 void vect2_add_cart(vect2_cart* vc1, vect2_cart* vc2, vect2_cart* vresult)
51 {
52         vresult->x = vc1->x + vc2->x;
53         vresult->y = vc1->y + vc2->y;
54         
55         return;
56 }
57
58
59 /* Substract 2 polar vectors and return the result */
60 void vect2_sub_pol(vect2_pol* vp1, vect2_pol* vp2, vect2_pol* vresult)
61 {
62         vect2_cart vc1;
63         vect2_cart vc2;
64         
65         vect2_cart vc;
66         
67         vect2_pol2cart(vp1,&vc1);
68         vect2_pol2cart(vp2,&vc2);
69         
70         vect2_sub_cart(&vc1,&vc2,&vc);
71         
72         vect2_cart2pol(&vc,vresult);
73         
74         return;
75 }
76
77 /* Substract 2 cartesian vectors and return the result */
78 void vect2_sub_cart(vect2_cart* vc1, vect2_cart* vc2, vect2_cart* vresult)
79 {
80         vresult->x = vc1->x - vc2->x;
81         vresult->y = vc1->y - vc2->y;
82         
83         return;
84 }
85
86
87 /* Multiply a cartesian vector by a scalar and return the result */
88 void vect2_scale_cart(vect2_cart* vc1, Real alpha, vect2_cart* vresult)
89 {
90         vresult->x = alpha*(vc1->x);
91         vresult->y = alpha*(vc1->y);
92         
93         return;
94 }
95
96 /* Multiply a polar vector by a scalar and return the result */
97 void vect2_scale_pol(vect2_pol* vp1, Real alpha, vect2_pol* vresult)
98 {
99         vresult->r = alpha*vp1->r;
100
101         return;
102 }