習作①
ソース
なぜかx1,y1,x2,y2,x3,y3
がグローバルにしないとvoid nns(){}
関数の中だと適用されなかった。
多分だけどvoid nns(){}
の名前空間の中だけの適用だからvoid draw(){}
に持ってったらエラーというか書けなくなるのかな
わからん。。
int NUM = 200; float x1; float y1; float x2; float y2; float x3; float y3; Particles[] particle= new Particles[NUM]; void setup(){ size(800,800); for (int i = 0; i < NUM ; i++){ particle[i] = new Particles(); } } void draw(){ background(255); for(int i = 0; i< NUM ; i ++){ particle[i].display(); particle[i].step(); } nns(); } void nns(){ for(int j = 0; j < NUM; j++){ float[] distances = new float[NUM-1]; for(int i =0; i< NUM-1 ; i++){ distances[i] = dist(particle[j].location.x,particle[j].location.y,particle[i].location.x,particle[i].location.y); } distances= sort(distances); float saitan1 = distances[1]; float saitan2 = distances[2]; float saitan3 = distances[3]; for(int i = 0; i < NUM-1 ; i++){ if(dist(particle[j].location.x,particle[j].location.y,particle[i].location.x,particle[i].location.y) == saitan1){ x1 = particle[i].location.x; y1 = particle[i].location.y; } } for(int i = 0; i < NUM-1 ; i++){ if(dist(particle[j].location.x,particle[j].location.y,particle[i].location.x,particle[i].location.y) == saitan2){ x2 = particle[i].location.x; y2 = particle[i].location.y; } } for(int i = 0; i < NUM-1 ; i++){ if(dist(particle[j].location.x,particle[j].location.y,particle[i].location.x,particle[i].location.y) == saitan3){ x3 = particle[i].location.x; y3 = particle[i].location.y; } } fill(20,20); noStroke(); triangle(x1,y1,x2,y2,x3,y3); } }
クラス
class Particles { PVector location; PVector velocity; Particles(){ location = new PVector(random(0,width),random(0,height)); velocity = new PVector(random(-4,4),random(-4,4)); } void step(){ location.add(velocity); if((location.x<0)||(location.x>width)){ velocity.x *= -1; } if((location.y<0)||(location.y>height)){ velocity.y *=-1; } } void display(){ ellipse(location.x,location.y,5,5); } }