Advertisement
Guest User

double Function

a guest
Apr 12th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. abstract class Func {
  2.    
  3.     public abstract double apply(double x);
  4.    
  5.     public Func scale(double fx, double fy) {
  6.         final Func that = this;
  7.         return new Func() {
  8.             public double apply(double x) {
  9.                 return that.apply(x/fx)*fy;
  10.             }
  11.         };
  12.     }
  13.     public Func shift(double dx, double dy) {
  14.         final Func that = this;
  15.         return new Func() {
  16.             public double apply(double x) {
  17.                 return that.apply(x-dx)+dy;
  18.             }
  19.         };
  20.     }
  21.     public Func combine(double p, Func other) {
  22.         final Func that = this;
  23.         return new Func() {
  24.             public double apply(double x) {
  25.                 if(x <= p) return that.apply(x);
  26.                 else return other.apply(x);
  27.             }
  28.         };
  29.     }
  30.     public Func flipX() {
  31.         return scale(-1, 1).shift(1, 0);
  32.     }
  33.     public Func flipY() {
  34.         return scale(1, -1).shift(0, 1);
  35.     }
  36.     public Func flipXY() {
  37.         return scale(-1, -1).shift(1, 1);
  38.     }
  39.     public Func inOut() {
  40.         return scale(0.5, 0.5).combine(
  41.                    0.5,
  42.                    flipXY().scale(0.5, 0.5).shift(0.5, 0.5));
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement