Math Function Library
From AgeofWiki
These are math functions used in trigonometry and other forms of math. They were made for AoM by Wolfenhex with help of his math friends Bryan and Gabe. They also work in AoE3.
If you are afraid of the functions being too slow, you can change the for(i = 1; < 100) loop in each function to a lower value, but they will be less accurate. They have never gone all 100 loops before though, because the break syntax stops it from looping once the results are calculated.
All the functions have an underscore (_) before them because ES may add math functions later on, and we don't want to overwrite their built-in functions.
Please do not modify the order of these functions. They must be added to an RMS in this order.
| Table of contents |
PI
This is just a constant variable for pi. Never edit the value of this variable. There are only 6 decimal places because that is the most the RMS engine can support.
float PI = 3.141593;
Power (pow)
This function will return n raised to the x power.
float _pow(float n = 0,int x = 0) {
float r = n;
for(i = 1; < x) {
r = r * n;
}
return (r);
}
Arctangent (atan)
This function will return the arctangent (in radians) of n.
float _atan(float n = 0) {
float m = n;
if(n > 1) m = 1.0 / n;
if(n < -1) m = -1.0 / n;
float r = m;
for(i = 1; < 100) {
int j = i * 2 + 1;
float k = _pow(m,j) / j;
if(k == 0) break;
if(i % 2 == 0) r = r + k;
if(i % 2 == 1) r = r - k;
}
if(n > 1 || n < -1) r = PI / 2.0 - r;
if(n < -1) r = 0.0 - r;
return (r);
}
Arctangent #2 (atan2)
This function will return the angle (in radians) whose tangent is the quotient of z and x.
float _atan2(float z = 0,float x = 0) {
if(x > 0) return (_atan(z / x));
if(x < 0) {
if(z < 0) return (_atan(z / x) - PI);
if(z > 0) return (_atan(z / x) + PI);
return (PI);
}
if(z > 0) return (PI / 2.0);
if(z < 0) return (0.0 - (PI / 2.0));
return (0);
}
Factorial (fact)
This function will return the factorial of n.
float _fact(float n = 0) {
float r = 1;
for(i = 1; <= n) {
r = r * i;
}
return (r);
}
Cosine (cos)
This function will return the cosine (in radians) of n.
float _cos(float n = 0) {
float r = 1;
for(i = 1; < 100) {
int j = i * 2;
float k = _pow(n,j) / _fact(j);
if(k == 0) break;
if(i % 2 == 0) r = r + k;
if(i % 2 == 1) r = r - k;
}
return (r);
}
Sine (sin)
This function will return the sine (in radians) of n.
float _sin(float n = 0) {
float r = n;
for(i = 1; < 100) {
int j = i * 2 + 1;
float k = _pow(n,j) / _fact(j);
if(k == 0) break;
if(i % 2 == 0) r = r + k;
if(i % 2 == 1) r = r - k;
}
return (r);
}
