I thought I’d try to get some snowball rolling here.
Rules are simple: Post a single static method “Utils” method that you find useful. Hopefully we’ll have tons of methods in here that people can copy and paste to use in their own Utils classes.
These methods can be many things, but they deal with a set of parameters and return a single value.
Keep the name of the method very descriptive, not just “getBla”, but “calculateBlaFromSetOfBlahblahs”.
I’ll start with a couple of methods:
// Linear interpolation, get the value between a and b where t is 0-1.
public static float lerp(float a, float b, float t) {
if (t < 0)
return a;
return a + t * (b - a);
}
// Get the euclidean distance between two points.
public static float euclideanDistance(float x1, float y1, float x2, float y2) {
return (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
Both of these can probably be optimized, so go ahead and post your optimized version.