nick /224 /soundeatslight

/*by nik hanselmann */
ArrayList critters = new ArrayList();
ArrayList critter_food = new ArrayList();

float time_count;
float critter_delay = 100;

simulated_data light;
simulated_data sound_level;
simulated_data temperature;

void setup(){
  size(800,600);

  light = new simulated_data(800,0,430,5,200,.001); //(number of data points in data array, minimum data value, max data value, minimum variance, max variance over time, pace of sin wave)
  sound_level = new simulated_data(800,0,17,17,5,.01);
  temperature = new simulated_data(800,20,25,.2,1, .01);
  sound_level.add_noise(100, 50, 200, sound_level.high);
  temperature.add_noise(60, 10, 100, 2);
  light.add_noise(20,50,200,100);

  /*sound_level.generate_datapoint();
  light.generate_datapoint();
  temperature.generate_datapoint();*/

  smooth();
  noStroke();
  critters.add(new critter(sound_level.generate_datapoint()));
  critter_food.add(new food(0));
  time_count = millis();

}

void draw(){
  background(0);
  if(time_count <= millis()){
    time_count = millis() + critter_delay;
    println(critters.size() + ", " + critter_food.size());
    light.generate_datapoint();
    //sound_level.generate_datapoint();
    temperature.generate_datapoint();
    critters.add(new critter(sound_level.generate_datapoint()));
    for(int i = 0; i < map((int)light.data[light.data.length - 1],light.low, light.high,0,40); i++){
      critter_food.add(new food(random(width)));
    }
  }
  for(int i = 0; i < critters.size(); i++){
    critter c = (critter)critters.get(i);
    c.move();

    for(int j = 0; j < critter_food.size(); j++){
      food f = (food)critter_food.get(j);
      if(sqrt(sq(f.x - c.x) + sq(f.y - c.y)) <= c.r/2){
        critter_food.remove(j);
        c.r +=.3;
      }
            if(sqrt(sq(f.x - c.x) + sq(f.y - c.y)) <= c.r * 5){
      if(f.x < c.x){
        c.x -=c.speed;
      }
      if(f.x > c.x){
        c.x +=c.speed;
      }
      if(f.y < c.y){
        c.y -=c.speed;
      }
      if(f.y > c.y){
        c.y +=c.speed;
      }
            }
      //critter_food.set(i, f);
    }
    if((int)c.r <= 0){
      critters.remove(i);
    }
    else {
      critters.set(i, c);
    }
  }

  for(int i = 0; i < critter_food.size(); i++){
    food f = (food)critter_food.get(i);
    f.move();
    if(f.y >= height -10){
      critter_food.remove(i);
    } else{
    critter_food.set(i, f);
    }
  }

  for(int i = 0; i < critters.size(); i++){
    critter c = (critter)critters.get(i);
    ellipse(c.x, c.y, c.r, c.r);
  }
  for(int i = 0; i < critter_food.size(); i++){
    food f = (food)critter_food.get(i);
    ellipse(f.x, f.y, 1,1);
  }

}

class critter{
  float x, y,r,speed;
  critter(float size){
    x = random(width);
    y = random(height);
    r = size;
    speed = random(.01,.5);
  }
  void move(){
    r-= (r * .01);
  }
}

class food{
 float x, y;
 float fall_speed;
 food(float X){
   x = X;
   fall_speed = random(.01,1);
 }
 void move(){
   if(y <= height-5){
   y+=fall_speed;
   }
 }
}

class simulated_data{
  float[] data;
  float low, high, s_delta, l_delta;
  float s_period, l_period;
  float iteration, s_iteration, l_iteration;
  boolean noise;
  int noise_length; // how long noise happens
  int noise_frequency_max; // how often noise happens
  int noise_frequency_min;
  float noise_factor; // how much the value changes
  int noise_count; //count of how long noise has been applied
  float noise_value; //value of how much noise effects value
  simulated_data(int num_datapoints, float low_end, float high_end,
                 float short_change, float long_change,
                 /*float short_period,*/ float long_period){
    data = new float[num_datapoints];
    low = low_end;
    high = high_end;
    s_delta = short_change;
    l_delta = long_change;
    l_period = long_period;
    iteration = 0;
    s_iteration = 0;
    l_iteration = 0;
    noise = false;
    generate_datapoint();
  }
  float generate_datapoint(){
    int value_index = data.length - 1;
    float value;
    value = abs(sin(iteration)) * l_delta + random(-s_delta, s_delta) + low;
    if(noise == true){
      value = apply_noise(value);
    }
    value = map(value, low,high, low,high);
    shift_data();
    iteration+=l_period;
    data[value_index] = value;
    return value;
  }
  void shift_data(){
    for(int i = 1; i <= data.length - 1; i++){
      data[i - 1] = data[i];
    }
  }
  void draw(){
    for(int i = 1; i < data.length - 1; i++){
      line(i * (width/data.length), height - ((data[i]/high) * height),
           (i + 1) * (width/data.length), height - ((data[i + 1]/high) * height));
    }
  }
  void add_noise(int n_length, int n_frequency_min, int n_frequency_max, float n_factor){
    noise_length = n_length;
    noise_frequency_max = n_frequency_max;
    noise_frequency_min = n_frequency_min;
    noise_factor = n_factor;
    noise = true;
  }
  float apply_noise(float value){
    noise_count --;
    if(noise_count < 0){
      noise_count = (int)random(noise_frequency_min,noise_frequency_max);
      noise_value = random(-noise_factor,0);
    }
    value += noise_value;
    return value;
  }

}

Page Details
Contact DANM  |  Digital Arts and New Media  |  Arts Division  |  Grad Division
login