An offshoot of my other thread, but what are some are mathmatical functions that you want to see or use. Also, post any that you currently have scripted and I will add them to this first post.
I think I'm pretty good at math, but it's been almost two years since I've done anything beyond basic algebra. I started by looking at the C# MathHelper class, but they sadly don't show the whole function (at least that I'm able to find ATM)
~Trent
Initial post: int: AbsInt, MinInt, MaxInt float: AbsFloat, MinFloat, MaxFloat, DistanceFormula
int AbsInt(int value) { //Returns the Absolute Value of value.
if (value<0) {value=value*(-1);}
return value;
}
int MinInt(int value1, int value2) { //Returns the lesser of two int values.
if (value1>value2) return value1;
else return value2;
}
int MaxInt(int value1, int value2) { //Returns the greater of two int values.
return (value1>value2)*value1 + (value1<=value2)*value2;
}
int Factorial(int value) { //Returns value!
//Coming soon
}
float AbsFloat(this Maths*, float value) { //Returns the Absolute Value of value. |value|
if (value<0.0) {value=value*(-1.0);}
return value;
}
float MinFloat(this Maths*, float value1, float value2) { //Returns the lesser of two float values.
if (value1<value2) return value1;
else return value2;
}
float MaxFloat(this Maths*, float value1, float value2) { //Returns the greater of two float values.
return (value1>value2)*value1 + (value1<=value2)*value2;
}
float DistanceFormula(this Maths*, int x1, int y1, int x2, int y2) { //Returns the distance between the two points
float evalX, evalY, evalFinal;
evalX=Maths.RaiseToPower((IntToFloat(x2)-IntToFloat(x1)), 2.0);
evalY=Maths.RaiseToPower((IntToFloat(y2)-IntToFloat(y1)), 2.0);
evalFinal=Maths.Sqrt((evalX+evalY));
return evalFinal;
}
Just for reference:
Since true == 1 and false == 0, one could use:
int MaxInt(int a, int b) {
return (a>b)*a + (a<=b)*b;
}
Ooh goody. Didn't even think of that.
Also realized that I never put up the float ones (cause I meant to put them between different brackets).
For the Distance Forumula, I put ints as params, cause I'd use it mostly with player.x,y or mouse.x,y. But would it be better to use float parameters and make the end-user use IntToFloat all the time?
~Trent
When worrying about distances, its usually faster to square the constant you're comparing against than get the root of the square on the hypotenuse...
maybe someone can use this log function:
float log(float f) {
float log = 0.0;
float t = (f - 1.0) / (f + 1.0);
int i;
while (i < 100)
{
log += Maths.RaiseToPower(t, (2.0 * IntToFloat(i) + 1.0)) / (2.0 * IntToFloat(i) + 1.0);
i++;
}
log = log*2.0;
return log;
}
Here is a rounding function. It probably needs some tweaking, but I think it might be useful.
Can anyone think of a more efficient way to home in on value? The while loop might get lengthy, rounding big numbers to small numbers. There are probably other places where it could be trimmed down.
Also not sure if it will return the proper values for negative numbers.
float Round(float value, float roundTo, RoundDirection dir) {
if (roundTo==0) return 0;
if (roundTo<0) roundTo = roundTo * (-1);
if (value<0) roundTo = roundTo * (-1);
float a = roundTo;
while (a<value) {
a+=roundTo;
}
if (dir==eRoundDown) return a - (roundTo * (a =! value) )
if (dir==eRoundNearest) {
if (a - value <= value - (a - rounTo) ) return a;
else return a-roundTo;
}
if (dir==eRoundUp) return a;
}