First viz

Screen Shot 2014-07-19 at 18.35.22

ArrayList<Entity> nodes;

void setup(){

 size(800,400);

 // load the data into an array of strings
 String[] dati = loadStrings("datiOrgModel.csv" );
 
 // initialize the ArrayList which will contain our data
 nodes = new ArrayList<Entity>();
 
// loop through all the data of the file
 for(
   int i=0; 
   i<dati.length ; 
   i=i+1
 )
 {
    // take one line of data
    String line = dati[i];
 
   //split it into parts, with | as separator
   String[] parts = split( line , '|' );

   // part[0] contains the name
   // part[1] contains the type
   // part[2] contains the list of children 
  
   // Split the children of the current line, using ',' as separator
   String[] children = split( parts[2], ',');
 
   // create the new element to be visualized
   Entity newNode = new Entity( parts[0], parts[1] , children );
 
   // ass the node to the rest
   nodes.add( newNode );
 
 }

 
 


}



void draw(){
 
 // colour the background black
 background(0 );
 
 // cycle through all the nodes
 for(int i = 0; i<nodes.size(); i++){
   // get the i-th node
   Entity e1 = nodes.get( i );
   
   // cycle through all of its children
   for(int j= 0; j<e1.children.length ; j++){
 
      // take one of the children
      String child = e1.children[j]; 
 
     // search for it through all the nodes
    for(int k = 0; k<nodes.size(); k++){
     // take a node  
     Entity e2 = nodes.get( k );
 
     // is it the node we're looking for?
     if(e2.name.equals( child ) ){
 
       // yes!
       
      // we will calculate the gravity 
      // which will attract the node to its 'father'
      
      // copy the vector of the position of the child
      PVector v = new PVector();
      v.x = e2.position.x;
      v.y = e2.position.y;
 
      // subtract the position of father node
      v.sub( e1.position );
 
     // calculate the distance father-->child
     float magnitude = v.mag();
 
     // is the child still too far from father?
     if(magnitude>50){
        // create attraction (reverse it by multiplying it by -1,
        // and decrease it by multiplying it for 0.001
        v.mult( -0.001 );
       
       // add the force to the node, to create acceleration
       e2.addForce( v );
     }
 
 
    // draw the line from e1 to e2, to highlight the relation
    stroke(255,255,255);
    noFill();
    line(e1.position.x,e1.position.y, e2.position.x, e2.position.y );
   }
 
  }
 
  } 
 }
 
 
 // loop through the nodes and draw them
 for(int i = 0; i<nodes.size(); i++){
 
   Entity e = nodes.get( i );
   e.draw();
 
 }
 
 
 
}




// Entity Class
class Entity{

 // each entity contains a name, a type and some children
 String name;
 String type;
 String[] children;
 
 // the vectors define the position, speed and acceleration of the entity in the viz
 PVector position, speed, acceleration;
 
 
 // constructor
 public Entity(String n, String t, String[] c){
 
 name = n;
 type = t;
 children = c;
 
 // initially the entity is stopped (accel and speed = 0)
 // and it is in a random position on the screen
 position = new PVector();
 speed = new PVector(0,0);
 acceleration = new PVector(0,0);
 
 position.x = random( 0,width );
 position.y = random( 0,height);
 
 
   // for easier handling: no null, but empty array for children
   if(children==null){
    children = new String[0];
   }
 
 }
 
 
 void draw(){
 
 pushMatrix();
 
 // calculate friction on speed (proportional and inverse to speed)
 PVector frictionSpeed = new PVector();
 frictionSpeed.x = speed.x;
 frictionSpeed.y = speed.y;
 frictionSpeed.mult( -0.02 );
 addForce( frictionSpeed );
 // calculate friction on acceleration (proportional and inverse to acceleration)
 PVector frictionAcceleration = new PVector();
 frictionAcceleration.x = acceleration.x;
 frictionAcceleration.y = acceleration.y;
 frictionAcceleration.mult( -0.02 );
 addForce( frictionAcceleration );
 
 // add acceleration to speed and speed to position, to update the system
 speed.add( acceleration );
 position.add( speed );
 
 // after using it bring the acceleration to zero
 acceleration.x = 0;
 acceleration.y = 0;
 
 
 translate(position.x , position.y);
 
 // use the types of the entities to change form, colour...
 if(type.equals( "org" ) ){
 fill(0,255,0);
 } else if(type.equals( "people" ) ){
 fill(255,255,255);
 }
 
 noStroke();

 // draw the node
 ellipse(0,0,5,5);
 
 popMatrix();
 
 
 }

 
 // this function is used to add forces to the node, for acceleration
 void addForce( PVector v ){
 
 acceleration.add( v );
 
 }


}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s