-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathMath.h
302 lines (228 loc) · 5.89 KB
/
Math.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2013-1-24
* Update : 2019-1-18
* Author : scott.cgi
*/
#ifndef MATH_H
#define MATH_H
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <float.h>
#include <stdbool.h>
#include "Engine/Toolkit/Utils/Array.h"
#include "Engine/Toolkit/Platform/Log.h"
struct AMath
{
/**
* Test polygon contains 2D point.
* one point of polygon contains pair of x, y.
*
* return true inside or false outside.
*/
bool (*TestPolygonPoint) (Array(float)* polygon, float x, float y);
/**
* Test polygonA intersects polygonB.
*
* not test polygonB intersects polygonA.
* not test through and cross each others.
*
* return true inside or false outside.
*/
bool (*TestPolygonAB) (Array(float)* polygonA, Array(float)* polygonB);
/**
* Test polygonA and polygonB intersect each others.
* not test through and cross each others.
*
* return true inside or false outside.
*/
bool (*TestPolygonPolygon) (Array(float)* polygonA, Array(float)* polygonB);
/**
* Test polygonA intersects polygonB.
*
* not test polygonB intersects polygonA.
* can test through and cross each others.
*
* return true inside or false outside.
*/
bool (*TestPolygonABStrict) (Array(float)* polygonA, Array(float)* polygonB);
/**
* Test polygonA and polygonB intersect each others.
* can test through and cross each others.
*
* return true inside or false outside.
*/
bool (*TestPolygonPolygonStrict)(Array(float)* polygonA, Array(float)* polygonB);
/**
* Test lineA intersects lineB.
* not test lineB intersects lineA.
*
* return true inside or false outside.
*/
bool (*TestLineAB) (Array(float)* lineA, Array(float)* lineB);
/**
* Test lineA and lineB intersect each others.
*
* return true inside or false outside.
*/
bool (*TestLineLine) (Array(float)* lineA, Array(float)* lineB);
/**
* Rotate 2D points by angle.
* one point of pointArr contains pair of x, y.
*/
void (*RotatePoints) (Array(float)* pointArr, float angle, Array(float)* outRotatedPointArr);
};
extern struct AMath AMath[1];
//----------------------------------------------------------------------------------------------------------------------
#define GOLDEN_RATIO 0.618033988749894f
/**
* The value of (PI / 180.0f).
*/
#define DEGREE_TO_RADIAN 0.017453292519943f
/**
* The value of (PI / 360.0f).
*/
#define DEGREE_TO_RADIAN2 0.008726646259972f
/**
* The value of (180.0f / PI).
*/
#define RADIAN_TO_DEGREE 57.29577951308232f
#define MATH_PI 3.141592653589793f
/**
* The value of (2 * PI).
*/
#define MATH_2PI 6.283185307179586f
/**
* The value of (PI / 2).
*/
#define MATH_PI2 1.570796326794897f
//----------------------------------------------------------------------------------------------------------------------
/**
* Random float in range [0.0, 1.0].
*/
static inline float AMath_Random()
{
return (float) (rand() / (double) RAND_MAX);
}
/**
* Random integer in range [from, to].
*/
static inline int AMath_RandomInt(int from, int to)
{
return (from) + rand() % ((to) - (from) + 1);
}
/**
* Random float in range [from, to].
*/
static inline float AMath_RandomFloat(float from, float to)
{
return from + AMath_Random() * ((to) - (from));
}
/**
* Random seed by system time.
*/
static inline void AMath_RandomSeedByTime()
{
srand((unsigned) time(NULL));
}
/**
* Convert degree to radian.
*/
static inline float AMath_ToRadian(float degree)
{
return degree * DEGREE_TO_RADIAN;
}
/**
* Convert radian to degree.
*/
static inline float AMath_ToDegree(float radian)
{
return radian * RADIAN_TO_DEGREE;
}
/**
* Cos by degree.
*/
static inline float AMath_Cos(float degree)
{
return cosf(AMath_ToRadian(degree));
}
/**
* Sin by degree.
*/
static inline float AMath_Sin(float degree)
{
return sinf(AMath_ToRadian(degree));
}
/**
* Degree by atan2.
*/
static inline float AMath_Atan2(float x, float y)
{
return AMath_ToDegree(atan2f(y, x));
}
/**
* Degree by acosf.
*/
static inline float AMath_Acos(float ratio)
{
return AMath_ToDegree(acosf(ratio));
}
/**
* Degree by asinf.
*/
static inline float AMath_Asin(float ratio)
{
return AMath_ToDegree(asinf(ratio));
}
/**
* Compare float value equals.
*/
static inline bool AMath_IsFloatEqual(float x, float y)
{
return fabsf((x) - (y)) <= FLT_EPSILON;
}
/**
* Fast square inverse root float.
*/
static inline float AMath_InvSqrtf(float x)
{
union { float f; int i; } u = {x};
u.i = 0x5f3759df - (u.i >> 1);
return u.f * (1.5f - 0.5f * x * u.f * u.f);
}
/**
* Fast square root float.
* equals AMath_InvSqrtf(x) * x.
*/
static inline float AMath_Sqrtf(float x)
{
union { float f; int i; } u = {x};
u.i = 0x5f3759df - (u.i >> 1);
x *= u.f; // reduce one multiplication and increase one assignment
return x * (1.5f - 0.5f * x * u.f);
}
/**
* Min in x and y, macro can use generic parameter.
*/
#define AMath_Min(x, y) \
(((x) < (y)) ? (x) : (y))
/**
* Max in a and b, macro can use generic parameter.
*/
#define AMath_Max(x, y) \
(((x) > (y)) ? (x) : (y))
/**
* Clamp x in min and max, macro can use generic parameter.
*/
#define AMath_Clamp(x, min, max) \
(AMath_Min((max), AMath_Max((x), (min))))
#endif