2.3 Random and Noise Based Motion

Here are two examples based on the Sol LeWitt wall drawing with random points. In each of the examples below the points are set in motion. The example on the left uses a constant speed for the X and Y values of each point that reverses when the pint hist the edge of the sketch. The example on the right uses a noise map to determine the X and Y positions of each point.

 

FloatList xpos = new FloatList();
FloatList ypos = new FloatList();
FloatList xspeed = new FloatList();
FloatList yspeed = new FloatList();
float numcls = 100;

void setup(){
  size(800,800);
  for(int i = 0; i < numcls; i++){
    xpos.append(random(width));
    ypos.append(random(height));
    xspeed.append(random(-3,3));
    yspeed.append(random(-3,3));
  }
}

void draw(){
  background(0);
  stroke(255);
  strokeWeight(10);
  for(int i = 0; i < xpos.size(); i++){ 
    float x = xpos.get(i); 
    float y = ypos.get(i); 
    float xs = xspeed.get(i); 
    float ys = yspeed.get(i); 

    //Check to see if the point's new position is 
    //outside of the bounds of the scketch
    if(x + xs > width || x + xs < 0){ 
      xs = -xs; 
    } 
    if(y + ys > height || y + ys < 0){
      ys = -ys;
    }
    
    x = x + xs;
    y = y + ys;
    
    point(x,y);
    xpos.set(i,x);
    ypos.set(i,y);
    xspeed.set(i,xs);
    yspeed.set(i,ys);
  } 
}
FloatList xnoise = new FloatList();
FloatList ynoise = new FloatList();
float numcls = 100;

void setup(){
  size(800,800);
  for(int i = 0; i < numcls; i++){
    xnoise.append(random(20000));
    ynoise.append(random(20000));
  }
}

void draw(){
  background(0);
  stroke(255);
  strokeWeight(10);
  for(int i = 0; i < xnoise.size(); i++){
    noiseDetail(5,0.59);
    float nx = xnoise.get(i);
    float ny = ynoise.get(i);
    float x = noise(nx);
    float y = noise(ny);
    
    //remap the noise value so it is
    //between 0.0 and 1.0
    x = map(x,0.2,0.9,0,1.0);
    y = map(y,0.2,0.9,0,1.0);
    
    point(x*width,y*height);
    nx = nx + 0.001;
    ny = ny + 0.001;
    xnoise.set(i,nx);
    ynoise.set(i,ny);
  }
}

The two sketches below are the same as the sketches above with the addition of a nested loop that checks the distance between any point and all other points. If that distance is below a certain value a line between the points is drawn.

 

FloatList xpos = new FloatList();
FloatList ypos = new FloatList();
FloatList xspeed = new FloatList();
FloatList yspeed = new FloatList();
float numcls = 100;

void setup(){
  size(800,800);
  for(int i = 0; i < numcls; i++){
    xpos.append(random(width));
    ypos.append(random(height));
    xspeed.append(random(-3,3));
    yspeed.append(random(-3,3));
  }
}

void draw(){
  background(0);
  stroke(255);
  strokeWeight(10);
  for(int i = 0; i < xpos.size(); i++){ float x = xpos.get(i); float y = ypos.get(i); float xs = xspeed.get(i); float ys = yspeed.get(i); if(x + xs > width || x + xs < 0){ xs = -xs; } if(y + ys > height || y + ys < 0){
      ys = -ys;
    }
    
    x = x + xs;
    y = y + ys;
    
    point(x,y);
    xpos.set(i,x);
    ypos.set(i,y);
    xspeed.set(i,xs);
    yspeed.set(i,ys);
  } 
  strokeWeight(1);
  for(int i = 0; i < xpos.size(); i++){
    float x1 = xpos.get(i);
    float y1 = ypos.get(i);
    for(int j = 0; j < xpos.size(); j++){
      float x2 = xpos.get(j);
      float y2 = ypos.get(j);
      float dd = dist(x1,y1,x2,y2);
      if(dd < 100){
        line(x1,y1,x2,y2);
      }
    }
  }
}
FloatList xnoise = new FloatList();
FloatList ynoise = new FloatList();
FloatList xpos = new FloatList();
FloatList ypos = new FloatList();
float numcls = 100;

void setup(){
  size(800,800);
  for(int i = 0; i < numcls; i++){
    xnoise.append(random(20000));
    ynoise.append(random(20000));
  }
}

void draw(){
  background(0);
  stroke(255);
  strokeWeight(10);
  for(int i = 0; i < xnoise.size(); i++){
    noiseDetail(5,0.59);
    float nx = xnoise.get(i);
    float ny = ynoise.get(i);
    float x = noise(nx);
    float y = noise(ny);
    
    x = map(x,0.2,0.9,0,1.0);
    y = map(y,0.2,0.9,0,1.0);
    
    point(x*width,y*height);
    nx = nx + 0.001;
    ny = ny + 0.001;
    xnoise.set(i,nx);
    ynoise.set(i,ny);
    xpos.set(i,x*width);
    ypos.set(i,y*height);
  }
  strokeWeight(1);
  for(int i = 0; i < xnoise.size(); i++){
    float x1 = xpos.get(i);
    float y1 = ypos.get(i);
    for(int j = 0; j < xnoise.size(); j++){
      float x2 = xpos.get(j);
      float y2 = ypos.get(j);
      float dd = dist(x1,y1,x2,y2);
      if(dd < 100){
        line(x1,y1,x2,y2);
      }
    }
  }
}