> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.UJ8xVTkROOB/rev.15
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * Pendula (v2.05)
 * by  Oighen (2014/Jun)
 * mod GoToLoop
 *
 * forum.processing.org/two/discussion/5795/pendula
 * studio.processingtogether.com/sp/pad/export/ro.9yPdCEFDrKsJH/latest
 */

static final color BG  = -1;
static final short NUM = 20;

final Pendulum[] pendula = new Pendulum[NUM];

void setup() {
  size(800, 600, JAVA2D);
  frameRate(60);
  smooth(2);

  stroke(Pendulum.OUTLINE);
  strokeWeight(1.5);
  ellipseMode(CENTER);

  Pendulum.fx = width>>1;
  Pendulum.fy = height>>5;

  for (int i = 0; i != NUM; pendula[i] = new Pendulum(i++, NUM));
}

void draw() {
  background(BG);
  for (Pendulum p: pendula)  p.action();
}

static abstract class PendulumShare {
  static final color OUTLINE = 0;
  static final float ALFA = .2467;
  static final short DIAM = 50, VEL = 1, NEAR = 3;

  static int fx, fy;
}

final class Pendulum extends PendulumShare {
  final short c, l, k;
  short h = -1, s = 1, w = -1;

  Pendulum(int val, int qty) {
    c = (short) map(val, 0, qty, 050, 0300);
    l = (short) ((height>>2) + DIAM/NEAR * val);
    k = (short) (l>>VEL);
  }

  void action() {
    if (++w > k)  s *= -(w=1);

    float beta = ALFA - 2.0*ALFA/k * (h+=s);
    float sign = beta < 0.0? 1.0 : -1.0;
    beta = abs(beta);

    float x = fx + l*sin(beta)*sign;
    float y = fy + l*cos(beta);

    display(x, y);
  }

  protected void display(float x, float y) {
    line(fx, fy, x, y);
    fill(c);
    ellipse(x, y, DIAM, DIAM);
  }
}