Skip to content

Commit 8f97951

Browse files
committedJun 13, 2021
Math.cpp added
1 parent d8ee5ac commit 8f97951

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
 

‎Engine/Core/Math.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "Math.h"
2+
3+
namespace Voxeler{
4+
float Math::Radians(float degrees) { return degrees * static_cast<float>(0.01745329251994329576923690768489); }
5+
6+
float Math::Lerp(float start, float stop, float step) { float v = start; while (v != stop) { v = (stop * step) + (start * 1.0 - step); } return v;}
7+
8+
float Math::PI() { return 3.14159265359; }
9+
10+
float Math::Sine(int deg) {
11+
deg %= 360;
12+
float rad = deg * PI() / 180;
13+
float sin = 0;
14+
for (int i = 0; i < VOX_TERMS; i++) {
15+
sin += power(-1, i) * power(rad, 2 * i + 1) / fact(2 * i + 1);
16+
}
17+
return sin;
18+
}
19+
20+
float Math::Cos(int deg) {
21+
deg %= 360; // make it less than 360
22+
float rad = deg * PI() / 180;
23+
float cos = 0;
24+
25+
int i;
26+
for (i = 0; i < VOX_TERMS; i++) {
27+
cos += power(-1, i) * power(rad, 2 * i) / fact(2 * i);
28+
}
29+
return cos;
30+
}
31+
32+
float Math::power(float base, int exp) {
33+
if (exp < 0) {
34+
if (base == 0)
35+
return -0;
36+
return 1 / (base * power(base, (-exp) - 1));
37+
}
38+
if (exp == 0)
39+
return 1;
40+
if (exp == 1)
41+
return base;
42+
return base * power(base, exp - 1);
43+
}
44+
45+
int Math::fact(int n) {
46+
return n <= 0 ? 1 : n * fact(n - 1);
47+
}
48+
}

0 commit comments

Comments
 (0)