1 /*
2 * MyMath.h
3 * nlivarot
4 *
5 * Created by fred on Wed Jun 18 2003.
6 *
7 */
9 #ifndef my_math
10 #define my_math
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <inttypes.h>
15 #include <string.h>
16 //#include <iostream.h>
19 typedef struct vec2
20 {
21 double x, y;
22 } vec2;
25 typedef struct mat2
26 {
27 double xx, xy, yx, yy;
28 } mat2;
31 typedef struct vec2d
32 {
33 double x, y;
34 } vec2d;
37 typedef struct mat2d
38 {
39 double xx, xy, yx, yy;
40 } mat2d;
43 #define RotCCW(a) {\
44 double t = (a).x;\
45 (a).x = (a).y;\
46 (a).y = -t;\
47 }
49 #define RotCCWTo(a,d) {\
50 (d).x = (a).y;\
51 (d).y = -(a).x;\
52 }
54 #define RotCW(a) {\
55 double t = (a).x;\
56 (a).x = -(a).y;\
57 (a).y = t;\
58 }
60 #define RotCWTo(a,d) {\
61 (d).x = -(a).y;\
62 (d).y = (a).x;\
63 }
65 #define Normalize(a) { \
66 double _le = (a).x*(a).x+(a).y*(a).y; \
67 if ( _le > 0.0001 ) { \
68 _le = 1.0 / sqrt(_le); \
69 (a).x *= _le; \
70 (a).y *= _le; \
71 } \
72 }
74 #define L_VEC_Set(a,u,v) { \
75 a.x = u; \
76 a.y = v; \
77 }
80 #define L_VEC_Length(a,l) { \
81 l = sqrt(a.x*a.x+a.y*a.y); \
82 }
84 #define L_VEC_Add(a,b,r) { \
85 r.x = a.x+b.x; \
86 r.y = a.y+b.y; \
87 }
89 #define L_VEC_Sub(a,b,r) { \
90 r.x = a.x-b.x; \
91 r.y = a.y-b.y; \
92 }
94 #define L_VEC_Mul(a,b,r) { \
95 r.x = a.x*b.x; \
96 r.y = a.y*b.y; \
97 }
99 #define L_VEC_Div(a,b,r) { \
100 r.x = a.x/b.x; \
101 r.y = a.y/b.y; \
102 }
104 #define L_VEC_AddMul(a,b,c,r) { \
105 r.x = a.x+b.x*c.x; \
106 r.y = a.y+b.y*c.y; \
107 }
109 #define L_VEC_SubMul(a,b,c,r) { \
110 r.x = a.x-b.x*c.x; \
111 r.y = a.y-b.y*c.y; \
112 }
115 #define L_VEC_MulC(a,b,r) { \
116 r.x = a.x*(b); \
117 r.y = a.y*(b); \
118 }
120 #define L_VEC_DivC(a,b,r) { \
121 r.x = a.x/(b); \
122 r.y = a.y/(b); \
123 }
125 #define L_VEC_AddMulC(a,b,c,r) { \
126 r.x = a.x+b.x*c; \
127 r.y = a.y+b.y*c; \
128 }
130 #define L_VEC_SubMulC(a,b,c,r) { \
131 r.x = a.x-b.x*c; \
132 r.y = a.y-b.y*c; \
133 }
135 #define L_VEC_Cmp(a,b) ((fabs(a.y-b.y)<0.0000001)? \
136 ((fabs(a.x-b.x)<0.0000001)?0:((a.x > b.x)?1:-1)): \
137 ((a.y > b.y)?1:-1))
139 #define L_VAL_Cmp(a,b) ((fabs(a-b)<0.0000001)?0:((a>b)?1:-1))
141 #define L_VEC_Normalize(d) { \
142 double l=sqrt(d.x*d.x+d.y*d.y); \
143 if ( l < 0.00000001 ) { \
144 d.x=d.y=0; \
145 } else { \
146 d.x/=l; \
147 d.y/=l; \
148 } \
149 }
151 #define L_VEC_Distance(a,b,d) { \
152 double dx = a.x-b.x; \
153 double dy = a.y-b.y; \
154 d = sqrt(dx*dx + dy*dy); \
155 }
157 #define L_VEC_Neg(d) { \
158 d.x=d.x; d.y=-d.y; \
159 }
161 #define L_VEC_RotCW(d) { \
162 double t=d.x; d.x=d.y; d.y=-t; \
163 } \
165 #define L_VEC_RotCCW(d) { \
166 double t=d.x; d.x=-d.y; d.y=t; \
167 }
169 #define L_VAL_Zero(a) ((fabs(a)<0.00000001)?0:((a>0)?1:-1))
171 #define L_VEC_Cross(a,b,r) { \
172 r = a.x*b.x+a.y*b.y; \
173 }
175 #define L_VEC_Dot(a,b,r) { \
176 r = a.x*b.y-a.y*b.x; \
177 }
180 #define L_MAT(m,a,b) { \
181 c[0][0].Set(ica.x); c[0][1].Set(icb.x); c[1][0].Set(ica.y); c[1][1].Set(icb.y); \
182 }
184 #define L_MAT_Set(m,a00,a10,a01,a11) {m.xx = a00; m.xy = a01; m.yx = a10; m.yy = a11;}
186 #define L_MAT_SetC(m,a,b) {m.xx = a.x; m.xy = b.x; m.yx = a.y; m.yy = b.y;}
188 #define L_MAT_SetL(m,a,b) {m.xx = a.x; m.xy = a.y;m.yx = b.x; m.yy = b.y;}
190 #define L_MAT_Init(m) {m.xx=m.xy=m.yx=m.yy=0;}
192 #define L_MAT_Col(m,no,r) { \
193 if ( no == 0 ) { \
194 r.x = m.xx; \
195 r.y = m.yx; \
196 } \
197 if ( no == 0 ) { \
198 r.x = m.xy; \
199 r.y = m.yy; \
200 } \
201 }
203 #define L_MAT_Row(m,no,r) { \
204 if ( no == 0 ) { \
205 r.x = m.xx; \
206 r.y = m.xy; \
207 } \
208 if ( no == 0 ) { \
209 r.x = m.yx; \
210 r.y = m.yy; \
211 } \
212 }
214 #define L_MAT_Det(m,d) {d=m.xx*m.yy-m.xy*m.yx;}
216 #define L_MAT_Neg(m) {m.xx=-m.xx; m.xy=-m.xy; m.yx=-m.yx; m.yy=-m.yy;}
218 #define L_MAT_Trs(m) {double t=m.xy; m.xy=m.yx; m.yx=t;}
220 #define L_MAT_Inv(m) { \
221 double d; \
222 L_MAT_Det(m,d); \
223 m.yx =- m.yx; \
224 m.xy =- m.xy; \
225 double t=m.xx;m.xx=m.yy;m.yy=t; \
226 double inv_d = 1.0/d; \
227 m.xx *= inv_d; \
228 m.xy *= inv_d; \
229 m.yx *= inv_d; \
230 m.yy *= inv_d; \
231 }
233 #define L_MAT_Cof(m) { \
234 m.yx =- m.yx; \
235 m.xy =- m.xy; \
236 double t=m.xx; m.xx=m.yy; m.yy=t; \
237 }
239 #define L_MAT_Add(u,v,m) { \
240 m.xx=u.xx+v.xx; m.xy=u.xy+v.xy; m.yx=u.yx+v.yx; m.yy=u.yy+v.yy; \
241 }
243 #define L_MAT_Sub(u,v,m) { \
244 m.xx=u.xx-v.xx; m.xy=u.xy-v.xy; m.yx=u.yx-v.yx; m.yy=u.yy-v.yy; \
245 }
247 #define L_MAT_Mul(u,v,m) { \
248 mat2d r; \
249 r.xx = u.xx*v.xx+u.xy*v.yx; \
250 r.yx = u.yx*v.xx+u.yy*y.yx; \
251 r.xy = u.xx*v.xy+u.xy*v.yy; \
252 r.yy = u.yx*v.xy+u.yy*v.yy; \
253 m=r; \
254 }
256 #define L_MAT_MulC(u,v,m) { \
257 m.xx=u.xx*v; m.xy=u.xy*v; m.yx=u.yx*v; m.yy=u.yy*v; \
258 }
260 #define L_MAT_DivC(u,v,m) { \
261 double iv = 1.0/v; \
262 m.xx = u.xx*iv; m.xy=u.xy*iv; m.yx=u.yx*iv; m.yy=u.yy*iv; \
263 }
265 #define L_MAT_MulV(m,v,r) { \
266 vec2d t; \
267 t.x = m.xx*v.x+m.xy*v.y; \
268 t.y = m.yx*v.x+m.yy*v.y; \
269 r=t; \
270 }
272 #define L_MAT_TMulV(m,v,r) { \
273 vec2d t; \
274 t.x = m.xx*v.x+m.yx*v.y; \
275 t.y = m.xy*v.x+m.yy*v.y; \
276 r=t; \
277 }
281 #endif