SHARE
TWEET

Interpolations

a guest Mar 10th, 2014 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.treehouseelite.elib.math;
  2.  
  3. /**
  4.  *
  5.  * Initial Finished Implementation of Interpolation Methods <br />
  6.  * <br />
  7.  * Takes a value between 0 and 1 as its Alpha and calculate the interpolated value using <br />
  8.  * a mathematical function (Sinusoidal, Linear, Exponential, etc.).
  9.  *
  10.  * @author Matthew Crocco
  11.  *
  12.  */
  13. public abstract class Interpolate {
  14.  
  15.         public static final Interpolate pow2 = new Power(2);
  16.         public static final Interpolate pow2In = new PowerInto(2);
  17.         public static final Interpolate pow2Out = new PowerOut(2);
  18.        
  19.         public static final Interpolate pow3 = new Power(3);
  20.         public static final Interpolate pow3In = new PowerInto(3);
  21.         public static final Interpolate pow3Out = new PowerOut(3);
  22.        
  23.         public static final Interpolate pow4 = new Power(4);
  24.         public static final Interpolate pow4In = new PowerInto(4);
  25.         public static final Interpolate pow4Out = new PowerOut(4);
  26.        
  27.         public static final Interpolate pow5 = new Power(5);
  28.         public static final Interpolate pow5In = new PowerInto(5);
  29.         public static final Interpolate pow5Out = new PowerOut(5);
  30.        
  31.         public static final Interpolate exp5 = new Exponential(2, 5);
  32.         public static final Interpolate exp5In = new ExponentialInto(2, 5);
  33.         public static final Interpolate exp5Out = new ExponentialOut(2, 5);
  34.        
  35.         public static final Interpolate exp10 = new Exponential(2, 10);
  36.         public static final Interpolate exp10In = new ExponentialInto(2, 10);
  37.         public static final Interpolate exp10Out = new ExponentialOut(2, 10);
  38.        
  39.         public static final Interpolate elastic = new Elastic(2f, 10f);
  40.         public static final Interpolate elasticIn = new ElasticIn(2f, 10f);
  41.         public static final Interpolate elasticOut = new ElasticOut(2f, 10f);
  42.        
  43.         public static final Interpolate swing = new Swing(1.5f);
  44.         public static final Interpolate swingIn = new SwingIn(2f);
  45.         public static final Interpolate swingOut = new SwingOut(2f);
  46.        
  47.         public static final Interpolate bounce = new Bounce(4);
  48.         public static final Interpolate bounceIn = new BounceIn(4);
  49.         public static final Interpolate bounceOut = new BounceOut(4);
  50.        
  51.        
  52.         abstract public float apply(float a);
  53.        
  54.         public float apply(float start, float end, float a){
  55.                 return start + (end - start) * apply(a);
  56.         }
  57.        
  58.         public static final Interpolate linear = new Interpolate(){
  59.                 @Override
  60.                 public float apply(float a){
  61.                         return a;
  62.                 }
  63.         };
  64.        
  65.         public static final Interpolate fade = new Interpolate(){
  66.                 @Override
  67.                 public float apply(float a){
  68.                         return Maths.clamp(a*a*a*(a*(a*6 -15) + 10), 0, 1);
  69.                 }
  70.         };
  71.        
  72.         public static final Interpolate sine = new Interpolate(){
  73.                 @Override
  74.                 public float apply(float a){
  75.                         return (1-Maths.cos(a * Maths.PI/2))/2;
  76.                 }
  77.         };
  78.        
  79.         public static final Interpolate sineIn = new Interpolate(){
  80.                 @Override
  81.                 public float apply(float a){
  82.                         return 1 - Maths.cos(a * Maths.PI/2);
  83.                 }
  84.         };
  85.        
  86.         public static final Interpolate sineOut = new Interpolate(){
  87.                 @Override
  88.                 public float apply(float a){
  89.                         return Maths.sin(a * Maths.PI/2);
  90.                 }
  91.         };
  92.        
  93.         public static final Interpolate circle = new Interpolate(){
  94.                 @Override
  95.                 public float apply(float a){
  96.                         if (a <= 0.5f) {
  97.                                 a *= 2;
  98.                                 return (1 - Maths.sqrt(1 - a * a)) / 2;
  99.                         }
  100.                         a--;
  101.                         a *= 2;
  102.                         return (Maths.sqrt(1 - a * a) + 1) / 2;
  103.                 }
  104.         };
  105.        
  106.         public static final Interpolate circleIn = new Interpolate(){
  107.                 @Override
  108.                 public float apply(float a){
  109.                         return 1 - Maths.sqrt(1 - a*a);
  110.                 }
  111.         };
  112.        
  113.         public static final Interpolate circleOut = new Interpolate(){
  114.                 @Override
  115.                 public float apply(float a){
  116.                         a--;
  117.                         return Maths.sqrt(1 - a*a);
  118.                 }
  119.         };
  120.        
  121.         // -- Power Interpolation Classes -- //
  122.        
  123.         public static class Power extends Interpolate{
  124.  
  125.                 protected int power;
  126.                
  127.                 public Power(int power){
  128.                         this.power = power;
  129.                 }
  130.                
  131.                 @Override
  132.                 public float apply(float a) {
  133.                         if(a <= 0.5f) return Maths.pow(2*a, power)/2;
  134.                         else return Maths.pow((a - 1) * 2, power) / (power % 2 == 0 ? -2 : 2) + 1;
  135.                 }
  136.                
  137.         }
  138.        
  139.         public static class PowerInto extends Power{
  140.                 public PowerInto(int power){
  141.                         super(power);
  142.                 }
  143.                
  144.                 @Override
  145.                 public float apply(float a){
  146.                         return Maths.pow(a, power);
  147.                 }
  148.         }
  149.        
  150.         public static class PowerOut extends Power{
  151.                 public PowerOut(int power){
  152.                         super(power);
  153.                 }
  154.                
  155.                 @Override
  156.                 public float apply(float a){
  157.                         return Maths.pow((a-1), power) * (power % 2 == 0 ? -1 : 1) + 1;
  158.                 }
  159.         }
  160.        
  161.         //-- Exponential Interpolation Classes --//
  162.        
  163.         public static class Exponential extends Interpolate{
  164.                 final float value, power, min, scale;
  165.                
  166.                 public Exponential(float value, int power){
  167.                         this.value = value;
  168.                         this.power = power;
  169.                         min = Maths.pow(value, -power);
  170.                         scale = 1 / (1-min);
  171.                 }
  172.                
  173.                 @Override
  174.                 public float apply(float a){
  175.                         if (a <= 0.5f) return ((float)Math.pow(value, power * (a * 2 - 1)) - min) * scale / 2;
  176.                         return (2 - ((float)Math.pow(value, -power * (a * 2 - 1)) - min) * scale) / 2;
  177.                 }
  178.                
  179.         }
  180.        
  181.         public static class ExponentialInto extends Exponential{
  182.                 public ExponentialInto(float value, int power){
  183.                         super(value, power);
  184.                 }
  185.                
  186.                 @Override
  187.                 public float apply(float a){
  188.                         return ((float)Math.pow(value, power * (a - 1)) - min) * scale;
  189.                 }
  190.         }
  191.        
  192.         public static class ExponentialOut extends Exponential{
  193.                 public ExponentialOut(float value, int power){
  194.                         super(value, power);
  195.                 }
  196.                
  197.                 @Override
  198.                 public float apply(float a){
  199.                         return 1 - ((float)Math.pow(value, -power * a) - min) * scale;
  200.                 }
  201.         }
  202.        
  203.         //-- Elastic Interpolation Classes --//
  204.        
  205.         static public class Elastic extends Interpolate {
  206.                 final float value, power;
  207.  
  208.                 public Elastic (float value, float power) {
  209.                         this.value = value;
  210.                         this.power = power;
  211.                 }
  212.  
  213.                 @Override
  214.                 public float apply (float a) {
  215.                         if (a <= 0.5f) {
  216.                                 a *= 2;
  217.                                 return (float)Math.pow(value, power * (a - 1)) * Maths.sin(a * 20) * 1.0955f / 2;
  218.                         }
  219.                         a = 1 - a;
  220.                         a *= 2;
  221.                         return 1 - (float)Math.pow(value, power * (a - 1)) * Maths.sin((a) * 20) * 1.0955f / 2;
  222.                 }
  223.         }
  224.  
  225.         static public class ElasticIn extends Elastic {
  226.                 public ElasticIn (float value, float power) {
  227.                         super(value, power);
  228.                 }
  229.  
  230.                 @Override
  231.                 public float apply (float a) {
  232.                         return (float)Math.pow(value, power * (a - 1)) * Maths.sin(a * 20) * 1.0955f;
  233.                 }
  234.         }
  235.  
  236.         static public class ElasticOut extends Elastic {
  237.                 public ElasticOut (float value, float power) {
  238.                         super(value, power);
  239.                 }
  240.  
  241.                 @Override
  242.                 public float apply (float a) {
  243.                         a = 1 - a;
  244.                         return (1 - (float)Math.pow(value, power * (a - 1)) * Maths.sin(a * 20) * 1.0955f);
  245.                 }
  246.         }
  247.  
  248.         //-- Bounce Interpolation Classes --//
  249.  
  250.         static public class Bounce extends BounceOut {
  251.                 public Bounce (float[] widths, float[] heights) {
  252.                         super(widths, heights);
  253.                 }
  254.  
  255.                 public Bounce (int bounces) {
  256.                         super(bounces);
  257.                 }
  258.  
  259.                 private float out (float a) {
  260.                         float test = a + widths[0] / 2;
  261.                         if (test < widths[0]) return test / (widths[0] / 2) - 1;
  262.                         return super.apply(a);
  263.                 }
  264.  
  265.                 @Override
  266.                 public float apply (float a) {
  267.                         if (a <= 0.5f) return (1 - out(1 - a * 2)) / 2;
  268.                         return out(a * 2 - 1) / 2 + 0.5f;
  269.                 }
  270.         }
  271.        
  272.         static public class BounceOut extends Interpolate {
  273.                 final float[] widths, heights;
  274.  
  275.                 public BounceOut (float[] widths, float[] heights) {
  276.                         if (widths.length != heights.length)
  277.                                 throw new IllegalArgumentException("Must be the same number of widths and heights.");
  278.                         this.widths = widths;
  279.                         this.heights = heights;
  280.                 }
  281.  
  282.                 public BounceOut (int bounces) {
  283.                         if (bounces < 2 || bounces > 5) throw new IllegalArgumentException("bounces cannot be < 2 or > 5: " + bounces);
  284.                         widths = new float[bounces];
  285.                         heights = new float[bounces];
  286.                         heights[0] = 1;
  287.                         switch (bounces) {
  288.                         case 2:
  289.                                 widths[0] = 0.6f;
  290.                                 widths[1] = 0.4f;
  291.                                 heights[1] = 0.33f;
  292.                                 break;
  293.                         case 3:
  294.                                 widths[0] = 0.4f;
  295.                                 widths[1] = 0.4f;
  296.                                 widths[2] = 0.2f;
  297.                                 heights[1] = 0.33f;
  298.                                 heights[2] = 0.1f;
  299.                                 break;
  300.                         case 4:
  301.                                 widths[0] = 0.34f;
  302.                                 widths[1] = 0.34f;
  303.                                 widths[2] = 0.2f;
  304.                                 widths[3] = 0.15f;
  305.                                 heights[1] = 0.26f;
  306.                                 heights[2] = 0.11f;
  307.                                 heights[3] = 0.03f;
  308.                                 break;
  309.                         case 5:
  310.                                 widths[0] = 0.3f;
  311.                                 widths[1] = 0.3f;
  312.                                 widths[2] = 0.2f;
  313.                                 widths[3] = 0.1f;
  314.                                 widths[4] = 0.1f;
  315.                                 heights[1] = 0.45f;
  316.                                 heights[2] = 0.3f;
  317.                                 heights[3] = 0.15f;
  318.                                 heights[4] = 0.06f;
  319.                                 break;
  320.                         }
  321.                         widths[0] *= 2;
  322.                 }
  323.  
  324.                 @Override
  325.                 public float apply (float a) {
  326.                         a += widths[0] / 2;
  327.                         float width = 0, height = 0;
  328.                         for (int i = 0, n = widths.length; i < n; i++) {
  329.                                 width = widths[i];
  330.                                 if (a <= width) {
  331.                                         height = heights[i];
  332.                                         break;
  333.                                 }
  334.                                 a -= width;
  335.                         }
  336.                         a /= width;
  337.                         float z = 4 / width * height * a;
  338.                         return 1 - (z - z * a) * width;
  339.                 }
  340.         }
  341.  
  342.         static public class BounceIn extends BounceOut {
  343.                 public BounceIn (float[] widths, float[] heights) {
  344.                         super(widths, heights);
  345.                 }
  346.  
  347.                 public BounceIn (int bounces) {
  348.                         super(bounces);
  349.                 }
  350.  
  351.                 @Override
  352.                 public float apply (float a) {
  353.                         return 1 - super.apply(1 - a);
  354.                 }
  355.         }
  356.  
  357.         //-- Swing Interpolation Classes --//
  358.  
  359.         static public class Swing extends Interpolate {
  360.                 private final float scale;
  361.  
  362.                 public Swing (float scale) {
  363.                         this.scale = scale * 2;
  364.                 }
  365.  
  366.                 @Override
  367.                 public float apply (float a) {
  368.                         if (a <= 0.5f) {
  369.                                 a *= 2;
  370.                                 return a * a * ((scale + 1) * a - scale) / 2;
  371.                         }
  372.                         a--;
  373.                         a *= 2;
  374.                         return a * a * ((scale + 1) * a + scale) / 2 + 1;
  375.                 }
  376.         }
  377.  
  378.         static public class SwingOut extends Interpolate {
  379.                 private final float scale;
  380.  
  381.                 public SwingOut (float scale) {
  382.                         this.scale = scale;
  383.                 }
  384.  
  385.                 @Override
  386.                 public float apply (float a) {
  387.                         a--;
  388.                         return a * a * ((scale + 1) * a + scale) + 1;
  389.                 }
  390.         }
  391.  
  392.         static public class SwingIn extends Interpolate {
  393.                 private final float scale;
  394.  
  395.                 public SwingIn (float scale) {
  396.                         this.scale = scale;
  397.                 }
  398.  
  399.                 @Override
  400.                 public float apply (float a) {
  401.                         return a * a * ((scale + 1) * a - scale);
  402.                 }
  403.         }
  404. }
RAW Paste Data
Top