I always wanted these functions, so when I was bored today, I quickly wrote them.
Algebraic functions:
A^B
I'm only listing this here, as few know of it. A**B means A^B. You can use this to do cube roots (num**(1/3)), etc.
Note that for e^x, you should use Exp(x)
My own:
UT lacks a log function, but did have an ln (Loge). I exploited this fact to write this function. First paremeter is the number to take the log of, base is the log base (default 10, as a base of 0 is impossible).
Reasoning behind code:
Trigonometic:
Note that all trig functions shown here (and in unrealscript) are calculated with radians. Some constants to convert between them and other units:
URot is the unreal rotation unit, where a full rotation is 2^16 units (rotators use this unit).
Unrealscript has a sin, cos, and tan function, but unfortunately, the only inverse function is atan...
Inverse Sin function (pass the sin of an angle, and it returns the angle):
Like atan, it will return an angle between -pi and pi.
Reasoning:
I rely on the pythagorean identity ( sin^2 x + cos ^2 x = 1), as well as the fact that tan x is defined as sin x / cos x. The reasoning behind the formula should then be obvious. Also note that the domain is -1<=A<=1, due to sin's nature.
Inverse Cos function (pass the cos of an angle, and it returns the angle). This is very useful to get angles between vectors (Acos (v1 dot v2)):
Similar expaination to asin. Note that pi is added if A<0 to shift from 3rd quad to 1st.
More exact inverts.
The following functions allow you to pass ratios with two numbers. For every angle on a circle, with the exception of two per trig function, there is another angle that has the same sin, cos, or tan. Using ratios allows the exact angle to be returned.
ArcTangent2. Pass the adjacent triangle leg as X and the opposite leg as Y (or X compoent, Y compont of a triangle inscribed in a unit circle).:
Note that if Y and X are 0, it will return 0, although in reality there is no answer (no line = no angle).
ArcSin2: a more precise asin. Y=opposite leg, R=radius/hypotenuse. Of course if R is 0, no line exists, so it will return 0. Note that this assumes you put my asin code in your script.
ACos2: a more precise acos. X=adjecent leg, R=opposite. Other rules are similar to ASin2:
Well, inform me if this is useful or not I hope I was of service (of cource, you could have easily written these functions yourself, but whatever )
Algebraic functions:
A^B
Code:
native(170) static final operator(12) float ** ( float A, float B );
Note that for e^x, you should use Exp(x)
My own:
Code:
static final function float Logarithm (float a, optional float Base){
if (Base==0)
Base=10;
return Loge(a)/Loge(base);
}
UT lacks a log function, but did have an ln (Loge). I exploited this fact to write this function. First paremeter is the number to take the log of, base is the log base (default 10, as a base of 0 is impossible).
Reasoning behind code:
Code:
log (Base) a =Res
Base^Res = a
ln (Base^Res) = ln a
Res*ln(Base) = ln a
Res=(ln a)/(ln base)
Trigonometic:
Note that all trig functions shown here (and in unrealscript) are calculated with radians. Some constants to convert between them and other units:
Code:
Const RadianToDegree = 57.2957795131;
Const DegreeToRadian = 0.01745329252;
Const RadianToURot = 10430.3783505;
Const URotToRadian = 0.000095873799;
Unrealscript has a sin, cos, and tan function, but unfortunately, the only inverse function is atan...
Inverse Sin function (pass the sin of an angle, and it returns the angle):
Like atan, it will return an angle between -pi and pi.
Code:
static final function float ASin ( float A ){
if (A>1||A<-1) //outside domain!
return 0;
if (A==1) //div by 0 checks
return Pi/2.0;
if (A==-1)
return Pi/-2.0;
return ATan(A/Sqrt(1-Square(A)));
}
I rely on the pythagorean identity ( sin^2 x + cos ^2 x = 1), as well as the fact that tan x is defined as sin x / cos x. The reasoning behind the formula should then be obvious. Also note that the domain is -1<=A<=1, due to sin's nature.
Inverse Cos function (pass the cos of an angle, and it returns the angle). This is very useful to get angles between vectors (Acos (v1 dot v2)):
Code:
static final function float ACos ( float A ){
if (A>1||A<-1) //outside domain!
return 0;
if (A==0) //div by 0 check
return (Pi/2.0);
A=ATan(Sqrt(1.0-Square(A))/A);
if (A<0)
A+=Pi;
Return A;
}
More exact inverts.
The following functions allow you to pass ratios with two numbers. For every angle on a circle, with the exception of two per trig function, there is another angle that has the same sin, cos, or tan. Using ratios allows the exact angle to be returned.
ArcTangent2. Pass the adjacent triangle leg as X and the opposite leg as Y (or X compoent, Y compont of a triangle inscribed in a unit circle).:
Note that if Y and X are 0, it will return 0, although in reality there is no answer (no line = no angle).
Code:
final static function float ATan2(float Y,float X)
{
local float tempang;
if(X==0) { //div by 0 checks.
if(Y<0)
return -pi/2.0;
else if(Y>0)
return pi/2.0;
else
return 0; //technically impossible (nothing exists)
}
tempang=ATan(Y/X);
if (X<0)
tempang+=pi; //1st/3th quad
//normalize (from -pi to pi)
if(tempang>pi)
tempang-=pi*2.0;
if(tempang<-pi)
tempang+=pi*2.0;
return tempang;
}
ArcSin2: a more precise asin. Y=opposite leg, R=radius/hypotenuse. Of course if R is 0, no line exists, so it will return 0. Note that this assumes you put my asin code in your script.
Code:
final static function float ASin2(float Y,float Rad)
{
local float tempang;
if(Rad==0)
return 0; //technically impossible (no hypotenuse = nothing)
tempang=ASin(Y/Rad);
if (Rad<0)
tempang=pi-tempang; //lower quads
return tempang;
}
ACos2: a more precise acos. X=adjecent leg, R=opposite. Other rules are similar to ASin2:
Code:
final static function float ACos2(float X,float Rad)
{
local float tempang;
if(Rad==0)
return 0; //no possible angle
tempang=ACos(X/Rad);
if (X<0)
tempang*=-1; //left quads
return tempang;
}
Well, inform me if this is useful or not I hope I was of service (of cource, you could have easily written these functions yourself, but whatever )