Just a couple basic static mathematical helper methods that I've found useful for game development. Feel free to add some in replies for future visitors. I will do another post later this week with advanced helper methods from trig and calc. I will also do a geometry helper class as well.
Math Helpers
public class MathHelper {
public static float AbsoluteValue(float Value) {
if (Value < 0)
Value *= -1;
return Value;
}
public static float Hold(float Value, float Minimum, float Maximum) {
if (Value < Minimum)
Value = Minimum;
if (Value > Maximum)
Value = Maximum;
return Value;
}
public static float ToPower(float Base, int Exponent) {
float b = Base;
if (Exponent == 0) { b = 1; }
else if (Exponent > 0) {
for (int i = 0; i < Exponent; i++)
Base *= b;
}
else if (Exponent < 0) {
Exponent *= -1;
for (int i = 1; i < Exponent; i++)
Base *= b;
Base = 1 / Base;
}
return Base;
}
public static float SquareRoot(float Value) {
if (Value == 0) { return 0; }
float v = (Value / 2) + 1;
float v1 = (v + (Value / 2)) / 2;
while (v1 < v) {
v = v1;
v1 = (v + (Value / v)) / 2;
}
return v;
}
public static float[] QuadraticFormula(float a, float b, float c) {
Complex ca = new Complex(a, 0);
Complex cb = new Complex(b, 0);
Complex cc = new Complex(c, 0);
Complex Δ = (cb * cb) - (4 * ca * cc);
Complex r1 = (-cb + Complex.Sqrt(Δ) / (2 * ca));
Complex r2 = (-cb - Complex.Sqrt(Δ) / (2 * ca));
float[] r = new float[2];
if (r1.Imaginary == 0 && r2.Imaginary == 0)
r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Real)};
else if (r1.Imaginary == 0)
r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Imaginary)};
else if (r2.Imaginary == 0)
r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Real)};
else
r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Imaginary)};
return r;
}
public static float Square(float Value) { return Value * Value; }
public static float Cube(float Value) { return Square(Value) * Value; }
public static float CubedSquared_Added(float Value) { return Cube(Value) + Square(Value); }
public static float CubedSquared_Subtracted(float Value) { return Cube(Value) - Square(Value); }
public static float CubedSquared_Multiplied(float Value) { return Cube(Value) * Square(Value); }
public static float CubedSquared_Divided(float Value) { return Cube(Value) / Square(Value); }
public static float LowerValue(float Value1, float Value2) {
return Value1 < Value2 ? Value1 : Value2;
}
public static float GreaterValue(float Value1, float Value2) {
return Value1 > Value2 ? Value1 : Value2;
}
public static float PercentIncrease(float OriginalValue, float NewValue) {
return ((NewValue - OriginalValue) / OriginalValue) * 100;
}
public static float PercentDecrease(float OriginalValue, float NewValue) {
return ((OriginalValue - NewValue) / OriginalValue) * 100;
}
}
lxXTaCoXxl
26
Posting Whiz in Training
Ab000dy_85
-3
Junior Poster in Training
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
lxXTaCoXxl
26
Posting Whiz in Training
lxXTaCoXxl
26
Posting Whiz in Training
ddanbe
2,724
Professional Procrastinator
Featured Poster
lxXTaCoXxl
26
Posting Whiz in Training
lxXTaCoXxl
26
Posting Whiz in Training
skatamatic
371
Practically a Posting Shark
digitig
0
Newbie Poster
Momerath
1,327
Nearly a Senior Poster
Featured Poster
nmaillet
97
Posting Whiz in Training
skatamatic
371
Practically a Posting Shark
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.