// 最短の距離を求めるシミュレーション class Point { float x,y; int[] link; Point(float xx, float yy){ x = xx; y = yy; link = new int[3]; } } int WINWIDTH = 500; int WINHEIGHT = 500; int CONTROLHEIGHT = 30; Point[] points; int npoints; int selind; int ndots = 7; boolean control = false; void setup() { PFont fontA = loadFont("CourierNew36.vlw"); textFont(fontA, 20); size(WINWIDTH,WINHEIGHT+CONTROLHEIGHT); points = new Point[1000]; initialize(ndots); } void initialize(int n) { int i; for(npoints=0;npoints= npoints){ Point p2 = points[p2ind]; if(dist(p1,p2) < 1.0){ int c = (random(2) > 1.0 ? 1 : 0); int ind1; for(ind1=0;ind1<3;ind1++){ if(p1.link[ind1] == p2ind) continue; if(c-- == 0) break; } c = (random(2) > 1.0 ? 1 : 0); int ind2; for(ind2=0;ind2<3;ind2++){ if(p2.link[ind2] == p1ind) continue; if(c-- == 0) break; } if(p1.link[ind1] >= npoints){ Point p = points[p1.link[ind1]]; for(int k=0;k<3;k++){ if(p.link[k] == p1ind){ p.link[k] = p2ind; break; } } } if(p2.link[ind2] >= npoints){ Point p = points[p2.link[ind2]]; for(int k=0;k<3;k++){ if(p.link[k] == p2ind){ p.link[k] = p1ind; break; } } } int tmp = p1.link[ind1]; p1.link[ind1] = p2.link[ind2]; p2.link[ind2] = tmp; } } } } } float dist2(Point p1,Point p2) { return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); } float dist(Point p1,Point p2) { return sqrt(dist2(p1,p2)); } float total(){ float v = 0.0; for(int i=0;i