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

Daily Creative Coding

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

円の中の近傍探索

/**
* nearest search in circle
*
* @author aa_debdeb
* @date 2016/06/12
*/

int num = 3000;
ArrayList<PVector> reached;
ArrayList<PVector> unreached;

void setup(){
  size(500, 500);
  frameRate(300);
  reached = new ArrayList<PVector>();
  unreached = new ArrayList<PVector>();
  for(int i = 0; i < num; i++){
    float size = map(pow(random(1), 1.0 / 2.0), 0, 1, 0, 200);
    float angle = random(TWO_PI);
    unreached.add(new PVector(size * cos(angle), size * sin(angle)));
  }
  reached.add(unreached.remove(0));
  background(102, 9, 28);
  strokeWeight(2);
  stroke(255, 209, 23);
}

void draw(){
  if(unreached.size() != 0){
    translate(width / 2, height / 2);
    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;
        }
      }
    }
    line(minRp.x, minRp.y, minUp.x, minUp.y);
    reached.add(minUp);
    unreached.remove(minUp);
  }
}