// MeshLibDemo.pde - Demo of Lee Byron's Mesh library for // calculating and drawing Voronoi, Delaunay and Convex Hull // diagrams. // Uses a set of random points to calculate all // three diagrams, with one point responding to the mouse // position. Press space to reset points, press '1', '2' and // '3' to toggle display of the different diagrams. // // The library is included in this sketch, see the "libraries" // subfolder See http://leebyron.com/else/mesh/ for more // information about the library. // // Marius Watz - http://workshop.evolutionzone.com/ //------------------------------------------------------------- // IMPORTS //------------------------------------------------------------- import processing.opengl.*; import megamu.mesh.*; import peasy.*; //------------------------------------------------------------- // GLOBAL VARIABLES //------------------------------------------------------------- Voronoi myVoronoi; Delaunay myDelaunay; Hull myHull; //[854] = number of lines (854) //[0] = latitude //[1] = longitude //[2] = magnitude float[][] Datapoints; String[][] Datatexts; float[][] timedArray; //each array has 3 parameters float[][] myEdges; MPolygon myRegions[],myHullRegion; int col[]; String[] lines; float posX, posY; float startX,startY,endX,endY; float[][] regionCoordinates; boolean showVoronoi=false; boolean showDelaunay=false; boolean showHull=false; boolean showTest=true; PeasyCam cam; PFont dataFont; int tCounter = 0; //------------------------------------------------------------- // SET UP //------------------------------------------------------------- void setup() { size(1200,600,P3D); //size(screen.wdith, 675, OPENGL); //size(screen.width,screen.height, OPENGL); // initialize points and calculate diagrams frameRate(30); smooth(); // println("regions " + myRegions.length); //println("edges " + myEdges.length); // Setup our PeasyCam cam = new PeasyCam( this, 2000 ); // Where are we looking from? cam.setMinimumDistance( 0 ); // What's the closest we can get? cam.setMaximumDistance( 12000 ); // What's the furthest we can get? //setting up our fonts dataFont = createFont("TGL31034-1-24.vlw", 20); textFont(dataFont); //setting up our CSV file String fileName = "EarthquakeLocateTime.csv"; println(">loadingCSV data from: " + fileName + "."); lines = loadStrings(fileName); println(">Done loading. Here's the data:"); println(); //parse(lines); initMesh(); // col=new int[Datapoints.length]; // for(int i=0; i60) col[i]=color(random(30,100)); // // else col[i]=color(random(200,255)); // col[i] = color(random(50,100),random(50,100),random(50,100), 100); // } } //------------------------------------------------------------- // OUR DRAW LOOP //------------------------------------------------------------- void draw(){ background(#222222); if(myRegions==null) return; //scale(0.1,0.1,0.1); scale(2,2,2); translate(-width/2,-height/2,125); directionalLight(200,100,255, 0, -1, 0); pointLight(200,100,255,width/5, height/5, 500); pointLight(255,0,255,-width/2, -height/2, 100); ambientLight(255, 0,0, screen.width/2, screen.height/2,400); // draw Voronoi if(showVoronoi) { strokeWeight(1); stroke(0); for(int i=0; iwidth){ regionCoordinates[j][0] = width; } if(regionCoordinates[j][1]>height){ regionCoordinates[j][1] = height; } //drawing the triangle points vertex(regionCoordinates[j][0], regionCoordinates[j][1],0); vertex(Datapoints[i][0], Datapoints[i][1],map(Datapoints[i][2],0,10,0,600)); } vertex(regionCoordinates[regionCoordinates.length-1][0], regionCoordinates[regionCoordinates.length-1][1],0); vertex(Datapoints[i][0], Datapoints[i][1],map(Datapoints[i][2],0,10,0,600)); vertex(regionCoordinates[0][0], regionCoordinates[0][1],0); endShape(); } } // draw Voronoi as lines if(showDelaunay) { strokeWeight(2); stroke(255,0,255); for(int i=0; i60) col[i]=color(random(30,100)); // else col[i]=color(random(200,255)); col[i] = color(random(50,100),random(50,100),random(50,100), 100); } //col[0]=color(255,0,0); } } void keyPressed() { // use keys '1'-'3' to toggle display if(key=='1') showVoronoi=!showVoronoi; if(key=='2') showDelaunay=!showDelaunay; if(key=='3') showHull=!showHull; if(key=='4') showTest=!showTest; } //------------------------------------------------------------- // PARSING THE DATA //------------------------------------------------------------- // This parse() function's main job // is to send each line to the splitLine() function. // So parse() gets off easy by passing the buck to splitLine() // and splitLine() does the hard work. void parse( String[] lines ) { Datapoints = new float[lines.length][3]; Datatexts = new String[lines.length][2]; //loop through each of the lines(vertically) for(int i = 0; i < lines.length; i++ ) { String[] pieces = splitLine( lines[ i ] ); //loop through the columns of one line(pieces) for(int j = 0; j < pieces.length; j++) { if(j==0){ Datapoints[i][j] = map(float(pieces[j]),-90,90,0,width); print(Datapoints[i][j] + ", "); } if(j==1){ Datapoints[i][j] = map(float(pieces[j]),-180,180,0,height); print(Datapoints[i][j] + ", "); } if(j==2){ Datapoints[i][j] = float(pieces[j]); print(Datapoints[i][j] + ", "); } if(j==3 || j==4){ Datatexts[i][j-3] = pieces[j]; println(Datatexts[i][j-3] + ", "); } } //println(); }; }; // splitLine() breaks apart each line into separate Strings // and returns them as a String array. String[] splitLine( String line ) { char[] c = line.toCharArray(); ArrayList pieces = new ArrayList(); int prev = 0; boolean insideQuote = false; for( int i = 0; i < c.length; i ++ ) { if( c[ i ] == ',' ) { if( !insideQuote ) { // Whitespace must be trimmed between commas String s = new String( c, prev, i - prev ).trim(); pieces.add( s ); prev = i + 1; }; } else if( c[ i ] == '\"' ) { insideQuote = !insideQuote; }; }; if( prev != c.length ) { String s = new String( c, prev, c.length - prev ).trim(); pieces.add( s ); }; String[] outgoing = new String[ pieces.size() ]; pieces.toArray( outgoing ); scrubQuotes( outgoing ); return outgoing; }; // Parse quotes from CSV data. // Quotes around a column are common. // Actual double quotes (") are specified by two double quotes (""). void scrubQuotes( String[] array ) { for( int i = 0; i < array.length; i ++) { if( array[ i ].length() > 2 ) { // Remove quotes at start and end, if present. if( array[ i ].startsWith( "\"" ) && array[ i ].endsWith( "\"" )) { array[ i ] = array[ i ].substring( 1, array[ i ].length() - 1 ); }; }; // Replace doubled-up double quotes with one double quote. array[ i ] = array[ i ].replaceAll( "\"\"", "\"" ); }; };