-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmath.cpp
297 lines (297 loc) · 7.4 KB
/
gmath.cpp
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
#include "gmath.h"
/**
* @brief Return the sign value of a number
*
* @param n number in double
* @return int returns -1 if the number is negative, 0 if 0 and 1 if more than 0
*/
int gmath::sign(double n){
if(n==0) return 0;
return (n<0) ? -1:1;
}
/**
* @brief Determines which if the parameters has the lowest value
*
* @param a first value
* @param b second value
* @return double lowest value
*/
double gmath::min(double a, double b){
return (a<b) ? a:b;
}
/**
* @brief Determines which if the parameters has the highest value
*
* @param a first value
* @param b second value
* @return double highest value
*/
double gmath::max(double a, double b){
return (a>b) ? a:b;
}
/**
* @brief Generates a random number in range (Including minimum and maximum number in the range)
*
* @param r1 first value in the range
* @param r2 last value in the range
* @return long long return the generated random number
*/
long long gmath::irandom(long long r1, long long r2){
static unsigned long long seed=std::chrono::system_clock::now().time_since_epoch().count();
long long retVal;
seed ^= seed<<26;
seed ^= seed>>34;
seed ^= seed<<10;
long long range = abs(r1-r2)+1;
retVal= (seed%range) + min(r1,r2);
return retVal;
}
/**
* @brief Generates a random number in range (Including minimum and maximum number in the range) with decimals
*
* @param r1 first value in the range
* @param r2 last value in the range
* @return double return the generated random number
*/
double gmath::drandom(double a, double b){
return gmath::min(gmath::max(a,b),
(double)gmath::irandom(a,b)+
(double)gmath::irandom(0,gmath::pow(10,15))/gmath::pow(10,15));
}
/**
* @brief Calculates the remainder of a double divided by another
*
* @param val Value to divide
* @param div divisor
* @return double remainder of the division (val%div)
*/
double gmath::dmod(double val, double div){
return __builtin_fmod(val, div);
}
/**
* @brief Calculates factorial of the number passed in as parameter
*
* @param n number to calculate factorial for
* @return int return result of the factorial
*/
int gmath::factorial(int n){
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
/**
* @brief Returns the absolute value of a number
*
* @param Value number in double
* @return double returns absolute value in double
*/
double gmath::abs(double Value){
return (Value<0) ? Value*-1: Value;
}
/**
* @brief Calculates the sinus value from degrees
*
* @param angle angle in degrees
* @return double returns the sinus value in double
*/
double gmath::dsin(double angle){
int inv=1;
if(angle<0) inv=-1;
angle=gmath::abs(angle);
while(angle>=180){angle-=180;inv*=-1;}
angle*=GM_PI/180;
return __builtin_sin(angle)*inv;
}
/**
* @brief Calculates the cosinus value from degrees
*
* @param angle angle in degrees
* @return double returns the cosinus value in double
*/
double gmath::dcos(double angle){
int inv=1;
if(angle<0) inv=-1;
angle=gmath::abs(angle);
while(angle>=180){angle-=180;inv*=-1;}
angle*=GM_PI/180;
return __builtin_cos(angle)*inv;
}
/**
* @brief Calculates the tangent value from degrees
*
* @param angle angle in degrees
* @return double returns the tangent value in double
*/
double gmath::dtan(double angle){
int inv=1;
if(angle<0) inv=-1;
angle=gmath::abs(angle);
while(angle>=180){angle-=180;inv*=-1;}
angle*=GM_PI/180;
return __builtin_tan(angle)*inv;
}
/**
* @brief Calculate the angle from sinus value
*
* @param x value in sinus
* @return double angle in degrees
*/
double gmath::asin(double x){
return __builtin_asin(x)*180/GM_PI;
}
/**
* @brief Calculate the angle from cosinus value
*
* @param x value in cosinus
* @return double angle in degrees
*/
double gmath::acos(double x){
return __builtin_acos(x)*180/GM_PI;
}
/**
* @brief Calculate the angle from tangent value
*
* @param x value in tangent
* @return double angle in degrees
*/
double gmath::atan(double x){
return __builtin_atan(x)*180/GM_PI;
}
/**
* @brief Calculate the angle from cosinus and sinus values
*
* @param x value in cosinus
* @param y value in sinus
* @return double angle in degrees
*/
double gmath::atan2(double x, double y){
return __builtin_atan2l(x,y);
}
/**
* @brief Calculates the power of a number
*
* @param base The number to calculate power for
* @param exp exponent in double
* @return double return result in double
*/
double gmath::pow(double base, double exp){
return __builtin_pow(base, exp);
}
/**
* @brief Calculate the square root of a number
*
* @param n number to calculate the square root of
* @return double square root in double
*/
double gmath::sqrt(double n){
return __builtin_sqrt(n);
}
/**
* @brief Returns the square value of a number
*
* @param n value in double
* @return double result of the square
*/
double gmath::sqr(double n){
return n*n;
}
/**
* @brief Rounds a floating value upwards
*
* @param n value in double
* @return double returns the value rounded up
*/
double gmath::ceil(double n){
if(n>0){
n*=10;
int temp=n;
temp%=10;
if(temp>0) return floor(n/10)+1;
return floor(n/10);
}
return floor(n);
}
/**
* @brief Rounds a floating value downwards
*
* @param n value in double
* @return double returns the value rounded down
*/
double gmath::floor(double n){
return (double)((long long int)n);
}
/**
* @brief Rounds a value normally
*
* @param n value to round
* @return double returns rounded value
*/
double gmath::round(double n){
int temp;
if(n>0){
n*=10;
temp=n;
temp%=10;
if(temp>=5) return floor(n/10)+1;
return floor(n/10);
}
n*=10;
temp=n;
temp%=10;
if(temp<=-5) return floor(n/10)-1;
return floor(n/10);
}
/**
* @brief Convert radians to degrees
*
* @param radian value in radians
* @return double value in degrees
*/
double gmath::radtodeg(double radian){
return radian*180/GM_PI;
}
/**
* @brief Returns the angle between two points
*
* @param x1 x coords of the first point
* @param y1 y coords of the first point
* @param x2 x coords of the second point
* @param y2 y coords of the second point
* @return double returns angle in degrees
*/
double gmath::point_direction(double x1, double y1, double x2, double y2){
return 180-gmath::radtodeg(gmath::atan2(y1-y2,x1-x2));
}
/**
* @brief Calculates distance between two points
*
* @param x1 x coords of the first point
* @param y1 y coords of the first point
* @param x2 x coords of the second point
* @param y2 y coords of the second point
* @return double returns distance in double
*/
double gmath::point_distance(double x1, double y1, double x2, double y2){
return sqrt(gmath::abs(y2-y1)*gmath::abs(y2-y1)+gmath::abs(x2-x1)*gmath::abs(x2-x1));
}
/**
* @brief Calculates the length along the x-axis
*
* @param dist distance to calculate
* @param angle angle of the direction
* @return double the distance along x-axis
*/
double gmath::lengthdir_x(double dist, double angle){
return dist*gmath::dcos(angle);
}
/**
* @brief Calculates the length along the y-axis
*
* @param dist distance to calculate
* @param angle angle of the direction
* @return double the distance along y-axis
*/
double gmath::lengthdir_y(double dist, double angle){
return dist * -gmath::dsin(angle);
}