public class Tree { //size of the ellipses final float NODE_SIZE = 10; final float EDGE_LENGTH = 10; final float EDGE_STRENGTH = 0.2; final float SPACER_STRENGTH = 500; public int x, y; public boolean alive; //start the fractal going public boolean triggeredStart; private int numNodes; public int pieceWidth = 300; public int pieceHeight = 300; public int delayTime; public int maxNodes; float SBalpha = 100; ParticleSystem physics; Smoother3D centroid; public Tree(){ triggeredStart = false; alive = false; numNodes = 0; delayTime = 500; } public void treeSetup(int xx, int yy, boolean a) { maxNodes = 50; numNodes = 0; delayTime = 500; this.alive = a; this.x = xx; this.y = yy; ellipseMode( CENTER ); physics = new ParticleSystem( 0, 0.25 ); centroid = new Smoother3D( 0.8 ); initialize(); this.triggeredStart = true; SBalpha = 100; } public void treeDraw() { physics.tick( 1.0 ); if ( physics.numberOfParticles() > 1 ) updateCentroid(); centroid.tick(); pushMatrix(); translate(x,y); translate( -centroid.x(), -centroid.y() ); drawNetwork(); if(triggeredStart && numNodes < (maxNodes)){ addNode(); numNodes++; delayTime--; } popMatrix(); }// end treeDraw /********************************************************************************/ void drawNetwork() { if(this.alive){ // draw vertices noStroke(); for ( int i = 0; i < physics.numberOfParticles(); ++i ) { Particle v = physics.getParticle( i ); //narrow ellispe fill(101,231,254,SBalpha); ellipse( v.position().x(), v.position().y(), NODE_SIZE/3, NODE_SIZE*1.5 ); //wide ellipse fill(101,231,254,SBalpha); ellipse( v.position().x(), v.position().y(), NODE_SIZE*1.5, NODE_SIZE/3 ); //smallest ellipse fill(49,223,254,SBalpha); ellipse( v.position().x(), v.position().y(), NODE_SIZE/3.5, NODE_SIZE/3.5 ); } if(SBalpha <=0.0) { this.alive=false; } // draw edges stroke(101,231,254,SBalpha); 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(); } }// end drawNetwork /********************************************************************************/ 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, pieceHeight/(deltaY+50) ); else centroid.setTarget( xMin + 0.5*deltaX, yMin +0.5*deltaY, pieceWidth/(deltaX+50) ); }//end updateCentroid() /********************************************************************************/ 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 ); } } //end addSpacersToNode /********************************************************************************/ void makeEdgeBetween( Particle a, Particle b ) { physics.makeSpring( a, b, EDGE_STRENGTH, EDGE_STRENGTH, EDGE_LENGTH ); } //end makeEdgeBetween /********************************************************************************/ void initialize() { //physics.clear(); physics.makeParticle(); //centroid.setValue( 0, 0, 1.0 ); } //end initialize /********************************************************************************/ void addNode() { Particle p = physics.makeParticle(); Particle q = physics.getParticle( (int)random( 0, physics.numberOfParticles()-1) ); while ( q == p ) q = physics.getParticle( (int)random( 0, physics.numberOfParticles()-1) ); addSpacersToNode( p, q ); makeEdgeBetween( p, q ); p.moveTo( q.position().x() + random( -1, 1 ), q.position().y() + random( -1, 1 ), 0 ); } //end addNode /********************************************************************************/ }// end Tree class