class Node implements Comparable { float x; float y; float dx; float dy; float destx; float desty; boolean fixed; String label; int count; int radius; boolean visible = true; int spiralPhase = 1; //Which way we're moving around the spiral --- 1=right, 2=down, 3=left, 4=up float spiralMaxDistance = 1; //The max x or y distance from center Node(String label) { this.label = label; x = random(0, width); y = random(0, height); } void increment() { count++; } public int compareTo(Object o) { if (!(o instanceof Node)) throw new ClassCastException(); int oradius = ((Node) o).radius; if (oradius < radius) return 1; if (oradius > radius) return -1; return 0; } void relax() { if(visible) { float ddx = 0; float ddy = 0; for (int j = 0; j < nodeCount; j++) { Node n = nodes[j]; if (n != this) { float vx = x - n.x; float vy = y - n.y; float lensq = vx * vx + vy * vy; if (lensq == 0) { ddx += random(1); ddy += random(1); } else if (lensq < 1000*1000) { //orig. 100*100 ddx += vx / lensq; ddy += vy / lensq; } } } float dlen = mag(ddx, ddy) / 2; if (dlen > 0) { dx += ddx / dlen; dy += ddy / dlen; } } } void update() { if(visible) { if (!fixed) { x += constrain(dx, -5, 5); y += constrain(dx, -5, 5); } x = constrain(x, 10, width-10); y = constrain(y, 10, height-10); dx /= 2; dy /= 2; } } void ease() { if(visible) { // Calculates new x/y/z coords based on target coords and current coords // Uses easing factor for smooth animation x += (destx - x) * easing; y += (desty - y) * easing; } } void draw() { if(visible) { float sender = float (label); fill(sender*10, sender*30, sender*50); noStroke(); strokeWeight(.02); if(layoutMethodToggle) { pushMatrix(); translate(-width/2, -height/2); } radius = constrain(count, 1, height/10); ellipse(x, y, radius*2, radius*2); float w = textWidth(label); if (fixed || count > w+2) { fill(0); textAlign(CENTER, CENTER); text(label, x, y); } if(layoutMethodToggle) { popMatrix(); } } } void resetObject() { x = 0; y = 0; destx = 0; desty = 0; spiralPhase = 1; spiralMaxDistance = 1; } void moveInSpiral() { switch(spiralPhase) { case 1: destx += spacing + 1; //Move right if(destx >= spiralMaxDistance) { spiralPhase = 2; //Switch to next phase } break; case 2: desty += spacing + 1; //Move down if(desty >= spiralMaxDistance) { spiralPhase = 3; //Switch to next phase } break; case 3: destx -= spacing + 1; //Move left if(abs(destx) >= spiralMaxDistance) { spiralPhase = 4; //Switch to next phase } break; case 4: desty -= spacing + 1; //Move up if(abs(desty) >= spiralMaxDistance) { spiralPhase = 1; //Switch to next phase spiralMaxDistance++; } break; } } }