Advertisement
hamoid

bouncyEllipsoRect

Jun 4th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. // Original code by Jerome Herr found at
  2. // http://p5art.tumblr.com/post/120693457458/ellipsorect-code
  3.  
  4. // Bouncy effect by aBe, using Penner's easing equation
  5.  
  6. int num = 240, dotsSide = num/4, spaceBetween = 5, counter, frames = 180, toSave = 0;
  7. float x, y, radius, sz = 2;
  8. PVector[] dotsRect = new PVector[num];
  9. PVector[] dotsCircle = new PVector[num];
  10.  
  11. void setup() {
  12.   size(540, 540);
  13.   background(0);
  14.   fill(255);
  15.   noStroke();
  16.   for (int j=1; j<=4; j++) {
  17.     for (int i=0; i<dotsSide; i++) {
  18.       switch (j) {
  19.       case 1:
  20.         x = i*spaceBetween;
  21.         y = 0;
  22.         break;
  23.       case 2:
  24.         x = dotsSide*spaceBetween;
  25.         y = i*spaceBetween;
  26.         break;
  27.       case 3:        
  28.         x = dotsSide*spaceBetween - i*spaceBetween;
  29.         y = dotsSide*spaceBetween;
  30.         break;
  31.       case 4:
  32.         x = 0;
  33.         y = dotsSide*spaceBetween - i*spaceBetween;
  34.         break;
  35.       }
  36.       dotsRect[counter] = new PVector(x, y);
  37.       counter++;
  38.     }
  39.   }
  40.   radius = (dotsSide*spaceBetween)/2;
  41.   for (int i=0; i<num; i++) {
  42.     x = radius + cos(PI+PI/4+TWO_PI/num*i)*radius*.8;
  43.     y = radius + sin(PI+PI/4+TWO_PI/num*i)*radius*.8;
  44.     dotsCircle[i] = new PVector(x, y);
  45.   }
  46. }
  47.  
  48. void draw() {
  49.   background(0);
  50.   translate((width-2*radius)/2, (width-2*radius)/2);
  51.   for (int i=0; i<num; i++) {
  52.     float a = dotsCircle[i].heading();
  53.     float delta = 15 + 15 * cos(a);
  54.     float t = (frameCount + delta) % frames;
  55.     float lerpValue;
  56.     if (t < frames * 0.25) {
  57.       lerpValue = Bounce.easeOut(t, 0, 1, frames*0.25);
  58.     } else if (t < frames * 0.5) {
  59.       lerpValue = 1;
  60.     } else if (t < frames * 0.75) {
  61.       lerpValue = Bounce.easeOut(t-frames*0.5, 1, -1, frames*0.25);
  62.     } else {
  63.       lerpValue = 0;
  64.     }
  65.     PVector newV = PVector.lerp(dotsRect[i], dotsCircle[i], lerpValue);
  66.     ellipse(newV.x, newV.y, sz, sz);
  67.   }
  68.   if(toSave > 0) {
  69.     saveFrame("/tmp/frm#####.gif");
  70.     println(--toSave);
  71.   }
  72. }
  73. // https://github.com/jesusgollonet/processing-penner-easing
  74. public static class Bounce {
  75.   // time, beginning, change, duration
  76.   public static float  easeOut(float t, float b, float c, float d) {
  77.     if ((t/=d) < (1/2.75f)) {
  78.       return c*(7.5625f*t*t) + b;
  79.     } else if (t < (2/2.75f)) {
  80.       return c*(7.5625f*(t-=(1.5f/2.75f))*t + .75f) + b;
  81.     } else if (t < (2.5/2.75)) {
  82.       return c*(7.5625f*(t-=(2.25f/2.75f))*t + .9375f) + b;
  83.     } else {
  84.       return c*(7.5625f*(t-=(2.625f/2.75f))*t + .984375f) + b;
  85.     }
  86.   }
  87. }
  88. void keyPressed() {
  89.   if(key == 's') {
  90.     toSave = frames;
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement