コード見ないで模写
OpenProcessingでこのスケッチを見て、模写してみたくなったのでコード見ないで書いてみた。それほどうまく真似れていないけどタイムアップ。コード見ないで模写は絵画のデッサンに似ている感じがする。
フロッキング(群体シミュレーション)はNature of Codeに載っているので、読み直す必要がある。
ArrayList<Mover> movers = new ArrayList<Mover>(); int num = 100; // setup関数 : 初回1度だけ実行される void setup() { fullScreen(P2D); //blendMode(SUBTRACT); colorMode(HSB, 360, 100, 100, 100); // HSBでの色指定にする smooth(); // 描画を滑らかに for (int i = 0; i < num; i++) { Mover m = new Mover(); movers.add(m); } background(0, 0, 0); } // draw関数 : setup関数実行後繰り返し実行される void draw() { fill(0, 0, 0, 5); rect(0, 0, width, height); for (Mover m : movers) { m.update(); m.draw(); } } class Mover { PVector pos; PVector vel; PVector acc; float theta; color c; float size; Mover() { //pos = new PVector(random(width), random(height)); pos = new PVector(width/2+random(-100,100), height/2+random(-100,100)); vel = new PVector(0, 0); theta = random(TWO_PI); float len = random(0.001, 0.01); acc = new PVector(cos(theta)*len, sin(theta)*len); size = random(5, 10); int r = (int)random(4); c = color(r*360/4, 80, 100); } PVector calcAcc(ArrayList<Mover> movers) { PVector newVel = new PVector(); int n = 1; for (Mover m : movers) { if (pos.equals(m.pos) != true) { float d = PVector.dist(pos, m.pos); if (d < 100) { float t = atan2(pos.y-m.pos.y, pos.x-m.pos.x); d = min(100, d); PVector v = new PVector(cos(t)*d, sin(t)*d); newVel.add(v); n++; } } } newVel.div(n); return newVel; } void update() { PVector newAcc = calcAcc(movers); acc.add(newAcc); vel.add(acc); vel.limit(2); pos.add(vel); if (pos.x < 0) { pos.x += width; } if (pos.y < 0) { pos.y += height; } if (pos.x > width) { pos.x -= width; } if (pos.y > height) { pos.y -= height; } theta += 0.01; } void draw() { pushMatrix(); translate(pos.x, pos.y); rotate(theta); fill(c); //stroke(0, 0, 100); //strokeWeight(0.1); noStroke(); ellipse(0, 0, size, size); popMatrix(); } }