import java.util.HashMap; //an object for handling interpolation using a variety of functions class Smoothing{ HashMap types; int count=13; String[] typeNames=new String[]{"linear", "quadIn","quadOut","quadInOut", "cubicIn","cubicOut","cubicInOut", "exIn","exOut","exInOut", "sinIn","sinOut","sinInOut"}; //init the hashmap mapping types to ints to allow you to call a function by name Smoothing(){ types=new HashMap(count); for(int i=0;i<13;i++) types.put(typeNames[i],i); } //The interpolate functions interpolate between a and b //if no range is given a range of 0-1 is assumed //the type can be either the string or int function identifier //the return value is always in the range a-b, or 0 if the function wasn't found float interpolate(String type,float a,float b,float x,float range){ return a+fn(type,x,range)*(b-a); } float interpolate(String type,float a,float b,float x){ return a+fn(type,x)*(b-a); } float interpolate(int type,float a,float b,float x,float range){ return a+fn(type,x,range)*(b-a); } float interpolate(int type,float a,float b,float x){ return a+fn(type,x)*(b-a); } //The fn functions allow abstracted access to the //if no range is given a range of 0-1 is assumed //the type can be either the string or int function identifier //the return value is always 0-1, 0 if the function isn't found float fn(String type,float x,float range){ return fn(type,x/range); } float fn(int type,float x,float range){ return fn(type,x/range); } float fn(String type,float x){ if(types.containsKey(type)) return fn(fnForName(type),x); return 0; //type not found } float fn(int type,float x){ x=constrain(x,0,1); switch(type){ case 0: return linear(x); case 1: return quadIn(x); case 2: return quadOut(x); case 3: return quadInOut(x); case 4: return cubicIn(x); case 5: return cubicOut(x); case 6: return cubicInOut(x); case 7: return exIn(x); case 8: return exOut(x); case 9: return exInOut(x); case 10: return sinIn(x); case 11: return sinOut(x); case 12: return sinInOut(x); } return 0; } //returns the int function identifier when given the string function name int fnForName(String n){ return ((Number)(types.get(n))).intValue(); } //returns the string name for a given int function identifier String nameForFn(int f){ return typeNames[f]; } //the following functions are used by float fn() float linear(float x){ return x; } float quadIn(float x){ return x*x; } float quadOut(float x){ x=1-x; return 1-x*x; } float quadInOut(float x){ if(x<0.5) return quadIn(x*2)/2; return quadOut((x-0.5)*2)/2+0.5; } float cubicIn(float x){ return x*x*x; } float cubicOut(float x){ x=1-x; return 1-x*x*x; } float cubicInOut(float x){ if(x<0.5) return cubicIn(x*2)/2; return cubicOut((x-0.5)*2)/2+0.5; } float exIn(float x){ return pow(2.71828183,(x-1)*8); } float exOut(float x){ return 1-exIn(1-x); } float exInOut(float x){ if(x<0.5) return exIn(x*2)/2; return exOut((x-0.5)*2)/2+0.5; } float sinIn(float x){ return 1-cos(x*PI/2); } float sinOut(float x){ return sin(x*PI/2); } float sinInOut(float x){ if(x<0.5) return sinIn(x*2)/2; return sinOut((x-0.5)*2)/2+0.5; } }