Creating shapes that change size through draw loops

edited December 2013 in Using Processing

I am trying to create a visualization where the nodes of my shape change size in a loop as the visualization progresses (I have stripped out the interactions between the nodes for simplicity here). I have the array sizes that is looped over in the draw function with index j. I am not sure why the nodes are not changing size.

And also if that problem has been solved, how can i make these shape move around randomly?

int numBalls = 5;
int sizeCounter = 0;
int everySoManyFramesChange = 3;

Ball[] balls = new Ball[numBalls];
float[] sizes = {
  15, 25, 35, 45, 55, 65
};

void setup() {
  size(800, 400);
  int l = 0 ;
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(30, 50), i, balls);
  }
  noStroke();
  fill(255, 204);
}

void draw() {
  background(0);
  for (int i = 0; i < numBalls; i++) {
    balls[i].diameter = sizes[sizeCounter];
    balls[i].display();
  }
  if (frameCount%everySoManyFramesChange == 0) sizeCounter=(sizeCounter+1)%sizes.length;
}
class Ball {
  float x, y;
  float diameter;
  float mass; 
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;

  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    mass = 50;
    id = idin;
    others = oin;
  } 

  void display() {
    textSize(32);
    fill(0, 255, 0, 255);
    ellipse(x, y, diameter, diameter);
    fill(255, 0, 0, 255);
    text(id, x, y);
  }
}

Answers

  • here....

    the size change worked already

    I added movement

    int numBalls = 5; 
    int sizeCounter = 0; 
    int everySoManyFramesChange = 3;
    
    Ball[] balls = new Ball[numBalls]; 
    float[] sizes = { 
      15, 25, 35, 45, 55, 65
    };
    
    void setup() { 
      size(800, 400); 
      int l = 0 ; 
      for (int i = 0; i < numBalls; i++) { 
        balls[i] = new Ball(random(width), random(height), random(30, 50), i, balls);
      } 
      noStroke(); 
      fill(255, 204);
    }
    
    void draw() { 
      background(0); 
      for (int i = 0; i < numBalls; i++) { 
        balls[i].diameter = sizes[sizeCounter]; 
        balls[i].move(); 
        balls[i].display();
      } 
      if (frameCount%everySoManyFramesChange == 0) 
        sizeCounter=(sizeCounter+1)%sizes.length;
    }
    
    // =========================================
    
    class Ball { 
      float x, y; 
      float diameter; 
      float mass; 
      float vx = 0;  // velocity
      float vy = 0; 
      int id; 
      Ball[] others;
    
      Ball(float xin, float yin, float din, int idin, Ball[] oin) { 
        x = xin; 
        y = yin; 
        diameter = din; 
        mass = 50; 
        id = idin; 
        others = oin;
        vx = random (-1.4, 1.4); // velocity
        vy = random (-1.4, 1.4);
      }
    
      void move() { 
        x+=vx;     // add velocity
        y+=vy;
      }
    
      void display() { 
        textSize(32); 
        fill(0, 255, 0, 255); 
        ellipse(x, y, diameter, diameter); 
        fill(255, 0, 0, 255); 
        text(id, x, y);
      }
    }
    
Sign In or Register to comment.