1 /*	Public domain	*/
2 
3 #ifndef _AGAR_GUI_MATH_H_
4 #define _AGAR_GUI_MATH_H_
5 
6 #include <agar/config/have_math.h>
7 #include <agar/config/have_math_c99.h>
8 #ifdef HAVE_MATH
9 #include <math.h>
10 #endif
11 
12 #include <agar/gui/begin.h>
13 
14 #ifdef M_PI
15 #define AG_PI M_PI
16 #else
17 #define	AG_PI 3.14159265358979323846
18 #endif
19 
20 #ifdef HAVE_MATH_C99
21 # define AG_Sin(x) sinf(x)
22 # define AG_Cos(x) cosf(x)
23 # define AG_Tan(x) tanf(x)
24 # define AG_Mod(x,y) fmodf((x),(y))
25 # define AG_Sqrt(x) sqrtf(x)
26 # define AG_Atan2(y,x) atan2f((y),(x))
27 # define AG_Floor(x) floorf(x)
28 # define AG_Ceil(x) ceilf(x)
29 # define AG_Fabs(x) fabsf(x)
30 #else
31 # define AG_Sin(x) ((float)sin((double)x))
32 # define AG_Cos(x) ((float)cos((double)x))
33 # define AG_Tan(x) ((float)tan((double)x))
34 # define AG_Mod(x,y) ((float)fmod((double)(x),(double)(y)))
35 # define AG_Sqrt(x) ((float)sqrt((double)x))
36 # define AG_Atan2(y,x) ((float)atan2((double)(y),(double)(x)))
37 # define AG_Floor(x) ((float)floor((double)x))
38 # define AG_Ceil(x) ((float)ceil((double)x))
39 # define AG_Fabs(x) ((float)fabs((double)x))
40 #endif /* HAVE_MATH_C99 */
41 
42 #define AG_Degrees(x) ((x)/(2.0*AG_PI)*360.0)
43 #define AG_Radians(x) (((x)/360.0)*(2.0*AG_PI))
44 #define AG_DotProd2(ax,ay,bx,by) ((ax)*(bx) + (ay)*(by))
45 #define AG_Norm2(ax,ay)	AG_Sqrt(AG_DotProd2((ax),(ay),(ax),(ay)))
46 #define AG_Distance2(ax,ay,bx,by) AG_Norm2((float)((ax)-(bx)),\
47                                            (float)((ay)-(by)))
48 
49 #if defined(_AGAR_INTERNAL) || defined(_USE_AGAR_GUI_MATH)
50 #define Sin(x) AG_Sin(x)
51 #define Cos(x) AG_Cos(x)
52 #define Tan(x) AG_Tan(x)
53 #define Mod(x,y) AG_Mod((x),(y))
54 #define Sqrt(x) AG_Sqrt(x)
55 #define Atan2(y,x) AG_Atan2((y),(x))
56 #define Floor(x) AG_Floor(x)
57 #define Ceil(x) AG_Ceil(x)
58 #define Fabs(x) AG_Fabs(x)
59 
60 #define Degrees(x) AG_Degrees(x)
61 #define Radians(x) AG_Radians(x)
62 #define DotProd2(ax,ay,bx,by) AG_DotProd2((ax),(ay),(bx),(by))
63 #define DotNorm2(ax,ay) AG_Norm2((ax),(ay))
64 #define Distance2(ax,ay,bx,by) AG_Distance2((ax),(ay),(bx),(by))
65 #define PowOf2i(x) AG_PowOf2i(x)
66 #define Hypot(x,y) AG_Hypot(x,y)
67 #define Truncf(x) AG_Truncf(x)
68 #define Fracf(x) AG_Fracf(x)
69 #define FracInvf(x) AG_FracInvf(x)
70 #endif /* _AGAR_INTERNAL or _USE_AGAR_GUI_MATH */
71 
72 __BEGIN_DECLS
73 static __inline__ int
AG_PowOf2i(int i)74 AG_PowOf2i(int i)
75 {
76 	int val = 1;
77 	while (val < i) { val <<= 1; }
78 	return (val);
79 }
80 static __inline__ float
AG_Hypot(float x,float y)81 AG_Hypot(float x, float y)
82 {
83 	return AG_Sqrt(x*x+y*y);
84 }
85 static __inline__ int
AG_Truncf(double d)86 AG_Truncf(double d)
87 {
88 	return ((int)floor(d));
89 }
90 static __inline__ double
AG_Fracf(double d)91 AG_Fracf(double d)
92 {
93 	return (d - floor(d));
94 }
95 static __inline__ double
AG_FracInvf(double d)96 AG_FracInvf(double d)
97 {
98 	return (1 - (d - floor(d)));
99 }
100 __END_DECLS
101 
102 #include <agar/gui/close.h>
103 #endif /* _AGAR_GUI_MATH_H_ */
104