Tut­o­r­i­a­l week 10

by Brittany Klaassens

floating circles

  • Show Code
/** @peep sketch **/
int NUM_CIRCLES = 100;
float MIN_RADIUS = 10;
float MAX_RADIUS = 20;
 
Circle[] circles;
 
void setup() {
  size(300, 300);
  frameRate(10);
  smooth();
  circles = new Circle[NUM_CIRCLES];
  for (int i = 0; i < circles.length; i++) {
    circles[i] = new Circle(random(width), random(height), random(MIN_RADIUS, MAX_RADIUS));
  }
}
 
void update() {
  for (int i = 0; i < circles.length; i++) {
    circles[i].update();
  }
}
 
void draw() {
  update();
  background(255);
  for (int i = 0; i < circles.length; i++) {
    circles[i].draw();
  }
}
 
class Circle {
  float x;
  float y;
  float radius;
 
  float heading;
  float speed;
 
  Circle(float _x, float _y, float _radius) {
    x = _x;
    y = _y;
    radius = _radius;
    heading = random(TWO_PI);
    speed = random (-5, 2);
  }
 
  void draw() {
    pushStyle();
    noFill();
    stroke(0);
    strokeWeight(1);
    ellipseMode(RADIUS);
    pushMatrix();
    translate(x, y);
    rotate(heading);
    ellipse(0, 0, radius, radius);
    line(0, 0, radius, 0);
    popMatrix();
    popStyle();
  }
 
  void update() {
    behaviour1();
  }
 
  void behaviour1() {
    // Constant linear motion
    float dx = speed * cos(heading);
    float dy = speed * sin(heading);
    x += dx;
    y += dy;
  }
}

Comments

Nobody has said anything yet.