Pendula

edited April 2016 in Share Your Work
int np=20; // numero pendoli - number of pendula
int cc; // contatore - counter
int [] l = new int[np]; // lunghezze filo - thread's lenght
float xF=500, yF=20; // coordinate del punto "fisso" - fixed point coordinates
float r=50; // raggio della sfera - radius of the sphere
float alfa=0.2467;  // angolo iniziale in radianti - starting angle in radiants
int [] k = new int[np]; // numero delle suddivisioni - number of divisions
float beta; // angolo variabile - variable angle
float xP,yP; // centro del pendolo - center of the shere
float step;
int [] h = new int[np]; // contatore - counter
int [] s = new int[np]; // contatore - counter
int [] w = new int[np]; // contatore - counter
int [] cl = new int[np]; // colori - colors
float temp;

void setup(){
  size(1000,700);
  background(255);
  for (cc=0;cc<np;cc++) {l[cc]=250+cc*int(r/5);h[cc]=-1;s[cc]=1;w[cc]=-1;cl[cc]=7*cc+40;}
  frameRate(200);
  }

void draw () {
 background(255);
 for (cc=0;cc<np;cc++){
  k[cc]=int(5.7*sqrt(l[cc])); 
  step=2*alfa/k[cc];
  w[cc]=w[cc]+1; if (w[cc]>k[cc]){w[cc]=1; s[cc]=-1*s[cc];}
  h[cc]=h[cc]+s[cc];
  temp=alfa-h[cc]*step;
  if (temp>=0)
     {beta=temp; xP=xF-l[cc]*sin(beta); yP=yF+l[cc]*cos(beta);}
  else  
     {beta=-1*temp; xP=xF+l[cc]*sin(beta); yP=yF+l[cc]*cos(beta);}

  line (xF,yF,xP,yP);
  fill (cl[cc]);
  ellipse (xP,yP,r,r);
  }
 }
Tagged:

Comments

  • I try to simulate the experiment shown in It's an attempt, I know the code needs to be improved. Any suggestion? This is my second sketch, so ... be patient.

  • Pendula

  • edited June 2014

    Hey! I've made a remixed version using class in place of multiple arrays. Read about it below:
    http://wiki.processing.org/w/From_several_arrays_to_classes

    You can view it in action online below:
    http://studio.processingtogether.com/sp/pad/export/ro.9yPdCEFDrKsJH/latest

    And here's the refactored code itself:

    /**
     * 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);
      }
    }
    
  • Thank a lot GoToLoop, I'll study your code. I have a lot to learn.

Sign In or Register to comment.