//create a _STATE_ array that will see if its poisoned or not //create a _HEALTH_ array that will record the size //write a _HEALTH INFLUENCE_ function that will modify the behavior of adjacent nodes //tweak settings import traer.physics.*; import traer.animation.*; final float NODE_SIZE = 10; final float EDGE_LENGTH = 20; final float EDGE_STRENGTH = 0.2; final float SPACER_STRENGTH = 1000; //controls disease behavior float[] oneEndHealth; float[] otherEndHealth; float circRadius; float floatWidth; float floatHeight; float newMouseX; float newMouseY; ParticleSystem physics; Smoother3D centroid; // PROCESSING ///////////////////////////////////// void setup() { oneEndHealth = new float[0]; otherEndHealth = new float[0]; circRadius = 50; newMouseX = 0; newMouseY = 0; size( 640, 640 ); floatWidth = width; floatHeight = height; smooth(); frameRate( 24 ); strokeWeight( 2 ); ellipseMode( CENTER ); physics = new ParticleSystem( 0, 0.25 ); centroid = new Smoother3D( 0.8 ); initialize(); } void draw() { if (physics.numberOfParticles() == 0) exit(); physics.tick( 1.0 ); if ( physics.numberOfParticles() > 1 ) updateCentroid(); centroid.tick(); background( 255 ); translate( width/2 , height/2 ); scale( centroid.z() ); translate( -centroid.x(), -centroid.y() ); stateUpdate(); drawNetwork(); addNode(); for (int i = 0; i < physics.numberOfSprings(); ++i) { Spring e = physics.getSpring(i); Particle a = e.getOneEnd(); Particle b = e.getTheOtherEnd(); float xPos = a.position().x(); float yPos = a.position().y(); newMouseX = (mouseX-(floatWidth/2))/centroid.z()+centroid.x(); newMouseY = (mouseY-(floatHeight/2))/centroid.z()+centroid.y(); if ( dist(xPos, yPos, newMouseX, newMouseY) < circRadius) { a.kill(); for( int k = 0 ; k < oneEndHealth.length - 1 ; ++k) { oneEndHealth[k] = oneEndHealth[k+1]; otherEndHealth[k] = otherEndHealth[k+1]; } b.kill(); } } circRadius +=.2; stroke(0); strokeWeight(2); fill(255); ellipse(newMouseX, newMouseY, circRadius,circRadius); } void drawNetwork() { // draw vertices fill( 160 ); noStroke(); for ( int i = 0; i < physics.numberOfSprings(); ++i ) { Spring e = physics.getSpring( i ); Particle a = e.getOneEnd(); Particle b = e.getTheOtherEnd(); float redHue1 = constrain(oneEndHealth[i]*2, 0, 255); float blueHue1 = constrain(255-oneEndHealth[i]*2, 0, 255); float redHue2 = constrain(oneEndHealth[i]*2, 0, 255); float blueHue2 = constrain(255-oneEndHealth[i]*2, 0, 255); fill(redHue1,0,blueHue1); ellipse( a.position().x(), a.position().y(), oneEndHealth[i], oneEndHealth[i] ); fill(redHue2,0,blueHue2); ellipse( b.position().x(), b.position().y(), otherEndHealth[i], otherEndHealth[i] ); } /*for ( int i = 0; i < physics.numberOfParticles(); ++i ) { Particle v = physics.getParticle( i ); ellipse( v.position().x(), v.position().y(), NODE_SIZE, NODE_SIZE ); }*/ // draw edges stroke( 0 ); beginShape( LINES ); for ( int i = 0; i < physics.numberOfSprings(); ++i ) { Spring e = physics.getSpring( i ); Particle a = e.getOneEnd(); Particle b = e.getTheOtherEnd(); vertex( a.position().x(), a.position().y() ); vertex( b.position().x(), b.position().y() ); } endShape(); } void keyPressed() { if ( key == 'c' ) { initialize(); return; } if ( key == ' ' ) { addNode(); return; } if ( key == 'k') { killNode(); return; } } // ME //////////////////////////////////////////// void updateCentroid() { float xMax = Float.NEGATIVE_INFINITY, xMin = Float.POSITIVE_INFINITY, yMin = Float.POSITIVE_INFINITY, yMax = Float.NEGATIVE_INFINITY; for ( int i = 0; i < physics.numberOfParticles(); ++i ) { Particle p = physics.getParticle( i ); xMax = max( xMax, p.position().x() ); xMin = min( xMin, p.position().x() ); yMin = min( yMin, p.position().y() ); yMax = max( yMax, p.position().y() ); } float deltaX = xMax-xMin; float deltaY = yMax-yMin; if ( deltaY > deltaX ) centroid.setTarget( xMin + 0.5*deltaX, yMin +0.5*deltaY, height/(deltaY+50) ); else centroid.setTarget( xMin + 0.5*deltaX, yMin +0.5*deltaY, width/(deltaX+50) ); /* float deltadeltaX = oldDeltaX - deltaX; float deltadeltaY = oldDeltaY - deltaY; newMouseX += ((mouseX-pmouseX)/floatWidth)*deltadeltaX-deltadeltaX/2; newMouseY += ((mouseY-pmouseY)/floatHeight)*deltadeltaY-deltadeltaY/2; float oldDeltaX = deltaX; float oldDeltaY = deltaY;*/ } void addSpacersToNode( Particle p, Particle r ) { for ( int i = 0; i < physics.numberOfParticles(); ++i ) { Particle q = physics.getParticle( i ); if ( p != q && p != r ) physics.makeAttraction( p, q, -SPACER_STRENGTH, 20 ); } } void makeEdgeBetween( Particle a, Particle b, int i, int j ) { physics.makeSpring( a, b, EDGE_STRENGTH, EDGE_STRENGTH, EDGE_LENGTH ); int healthLength = oneEndHealth.length; oneEndHealth = expand(oneEndHealth, healthLength+1); otherEndHealth = expand(otherEndHealth, healthLength+1); } void initialize() { physics.clear(); physics.makeParticle(); centroid.setValue( 0, 0, 1.0 ); } void addNode() { Particle p = physics.makeParticle(); int a = (int)random( 0, physics.numberOfParticles()-1); int b = (int)random( 0, physics.numberOfParticles()-1); Particle q = physics.getParticle( a ); while ( q == p ) q = physics.getParticle( b ); addSpacersToNode( p, q ); makeEdgeBetween( p, q, a, b ); p.moveTo( q.position().x() + random( -1, 1 ), q.position().y() + random( -1, 1 ), 0 ); } void killNode() { Particle p = physics.getParticle( (int)random( 0, physics.numberOfParticles()-1) ); p.kill(); } void stateUpdate() { for ( int i = 0; i < physics.numberOfSprings(); ++i ) { Spring e = physics.getSpring( i ); Particle a = e.getOneEnd(); Particle b = e.getTheOtherEnd(); oneEndHealth[i] += .25; otherEndHealth[i] += .25; } } void stateReset() { }