読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

ノイズ表面上での近傍探索

/**
* nearest search on noisy surface
*
* @author aa_debdeb
* @date 2016/06/13
*/

int num = 3000;
float noiseScale = 0.01;
ArrayList<PVector> reached;
ArrayList<PVector> unreached;

void setup(){
  size(500, 500);
  frameRate(300);
  reached = new ArrayList<PVector>();
  unreached = new ArrayList<PVector>();
  int n = 0;
  PVector noiseOffset = new PVector(random(10000), random(10000));
  while(n < num){
    PVector p = new PVector(random(width), random(height));
    if(random(1) < noise(p.x * noiseScale + noiseOffset.x, p.y * noiseScale + noiseOffset.y)){
      unreached.add(p);
      n++;
    }
  }
  reached.add(unreached.remove(0));
  background(0);
}

void draw(){
  if(unreached.size() != 0){
    float minD = 10000000;
    PVector minRp = null;
    PVector minUp = null;
    for(PVector rp: reached){
      for(PVector up: unreached){
        float d = PVector.dist(rp, up);
        if(d < minD){
          minD = d;
          minRp = rp;
          minUp = up;
        }
      }
    }
    strokeWeight(3);
    stroke(0, 0, 255, 100);
    line(minRp.x, minRp.y, minUp.x, minUp.y);
    strokeWeight(1);
    stroke(255, 100);
    line(minRp.x, minRp.y, minUp.x, minUp.y);
    reached.add(minUp);
    unreached.remove(minUp);
  }
}