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

About the Author

lxXTaCoXxl
26
Posting Whiz in Training

I'm a very difficult person to describe because I'm all over the place. If I could sum me up into one word it would be insane. I'm an incredibly vibrant person. I love surfing, playing guitar, programming, photography, video games, skating, snowboarding, running parkour, hanging out with friends, spending time with my girlfriend, and much more. I'm currently enrolled in college courses and pursuing jobs in my area. Ask if you want to know more.

```
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
Administrator
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 learning and sharing knowledge.