class network { int maxdots = 1000; int maxsprings = 1000; int maxfaces = 1000; int ndots, nsprings, nfaces; dot[] dots; spring[] springs; face[] faces; boolean[] dotvacant, springvacant, facevacant; boolean[][] friends; float[][] dotdist; float constructionScaleDistance, chargeScaleDistance, congealDistance; float randomness; float timestep = 0.05; // float beta = 1; // float mousebeta = 0.1; float k = 100; float chargeConstant = 0.045; float chargeMultiplier = 1.0; float congealiness = 0.7; float transparency = 0.9; float dotSize; color basecolor; network(float dist, float rand, color col) { dots = new dot[maxdots]; ndots = 0; springs = new spring[maxsprings]; nsprings = 0; faces = new face[maxfaces]; nfaces = 0; dotvacant = new boolean[maxdots]; for (int i=0; indots-1) {ndots = i+1;} dots[i] = new dot(x0,y0,ch,dotSize,col); return i; } else { return -1; } } int addSpring(int fromi, int toi, float k) { if ((fromi>-1) && (toi>-1)) { // find the first vacancy int i=0; while ((i<=maxsprings-1) && (!springvacant[i])) {i++;} // fill it with a new dot if (insprings-1) {nsprings = i+1;} springs[i] = new spring(dots[fromi],dots[toi],k); return i; } else { return -1; } } else { return -1; } } int addFace(dot[] v, color col) { // find the first vacancy int i=0; while ((i<=maxfaces-1) && (!facevacant[i])) {i++;} // fill it with a new dot if (infaces-1) {nfaces = i+1;} faces[i] = new face(v,col,transparency); return i; } else { return -1; } } void removeDot(int i) { dotvacant[i] = true; dots[i].dormant = true; for (int j=0; j7) { return yellowcolor; } else { return lighten(assigncolor(N)); } } else { return lighten(basecolor); } } void addPolygon(float x0, float y0, int N, color col) { int j; int[] n = new int[N]; float dtheta = TWO_PI/N; float rad = 2 * constructionScaleDistance/sqrt(2*(1-cos(TWO_PI/N))); float ch = 1; col = shift(col,random(randomness/4,randomness/4)); n[0] = addDot(x0+rad,y0,ch,darken(col)); for (int i=1; i-1) {springs[j].kpush = 0;} } j = addSpring(n[N-1],n[0],k); if (j>-1) springs[j].kpush = 0; for (int i=0; i-1) { for (int k=0; k-1) { friends[n[i]][n[k]] = true; friends[n[k]][n[i]] = true; } } } } dot[] v = new dot[N]; for (int i=0; i 0) { // calc distances for (int i=0; i chargeScaleDistance) { F = - chargeConstant * chargeMultiplier * dots[i].charge * dots[j].charge / pow(dotdist[i][j]/chargeScaleDistance,2.5); } else { F = - chargeConstant * chargeMultiplier * dots[i].charge * dots[j].charge / pow(dotdist[i][j]/chargeScaleDistance,3); } theta = atan2(dots[j].y-dots[i].y,dots[j].x-dots[i].x); if (abs(theta)<=TWO_PI) { dax = F*cos(theta); day = F*sin(theta); } else { dax = 0; day = 0; } dots[j].ax = dots[j].ax + dax; // note: assuming all dots have a mass of 1 dots[j].ay = dots[j].ay + day; dots[i].ax = dots[i].ax - dax; dots[i].ay = dots[i].ay - day; } } } for (int i=0; i