Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. PFont f;
  2.  
  3. float WIDTH = 1280;
  4. float HEIGHT = 720;
  5. float MARGIN = 50;
  6.  
  7. float MIN_LONG = 22;
  8. float MAX_LONG = 50;
  9.  
  10. float MIN_LAT = -128;
  11. float MAX_LAT = -67;
  12.  
  13. String[] zips;
  14. int zipNb;
  15.  
  16. float oldLong, oldLat, newLong, newLat;
  17. String firstDigits="01";
  18.  
  19. color c = color(random(0,255),random(0,255),random(0,255));
  20.  
  21. void setup(){
  22.   size(1280, 720);
  23.   frameRate(10000);
  24.   strokeWeight(1);
  25.  
  26.   zips = loadStrings("zips.txt");
  27.   zipNb = 1;
  28.   background(255);
  29.  
  30.   f = createFont("Calibri", 42);
  31.   textFont(f);
  32. }
  33.  
  34. void draw(){
  35.   //background(255);
  36.  
  37.   boolean connect = true;
  38.  
  39.   String[] zip = zips[zipNb].split("");
  40.   String firstTwoDigits = zip[0]+zip[1];
  41.   if(!firstTwoDigits.equals(firstDigits)){
  42.     c = color(random(0,255),random(0,255),random(0,255));
  43.     connect = false;
  44.     firstDigits = firstTwoDigits;
  45.   }
  46.  
  47.   connectZip(zipNb, connect);
  48.  
  49.   zipNb+=1;
  50.  
  51.   if(zipNb % 20 == 0){
  52.     // Save frame every 20 frames
  53.     //saveFrame("movie/######.png");
  54.   }
  55. }
  56.  
  57. void connectZip(int nb, boolean connect){
  58.  
  59.   if(nb>zips.length-1) exit();
  60.  
  61.   //println(zips[nb]);
  62.   String[] zip = zips[nb].split("\t");
  63.   fill(255);
  64.   stroke(255);
  65.   rect(25,0,150,50);
  66.   noStroke();
  67.   fill(0);
  68.  
  69.   newLong = Float.parseFloat(zip[1]);
  70.   newLat = Float.parseFloat(zip[2]);
  71.  
  72.   //print(newLong + "," + newLat);
  73.   if(connect){
  74.     drawLine(oldLong, oldLat, newLong, newLat);
  75.    
  76.     double length = Math.sqrt( Math.pow(newLong-oldLong,2) + Math.pow(newLat-oldLat,2) );
  77.     if(length > 10) println(zip);
  78.   }else{
  79.     drawLine(newLong, newLat, newLong, newLat);
  80.   }
  81.  
  82.   oldLong = newLong;
  83.   oldLat = newLat;
  84.  
  85. }
  86.  
  87.  
  88. void drawLine(float long1, float lat1, float long2, float lat2){
  89.   // Plot line on the graph
  90.  
  91.   if(long1<MIN_LONG || lat1<MIN_LAT) return;
  92.   if(long1>MAX_LONG || lat1>MAX_LAT) return;
  93.  
  94.   Point startPoint = mapPoint(long1, lat1);
  95.   Point endPoint = mapPoint(long2, lat2);
  96.  
  97.   stroke(c);
  98.   line(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
  99.  
  100. }
  101.  
  102. Point mapPoint(float longitude, float latitude){
  103.   // Converts a longitude-latitude pair into A,Y coords on the canvas
  104.  
  105.   float spanX = (HEIGHT-MARGIN*2) / (MAX_LONG-MIN_LONG);
  106.   float spanY = (WIDTH-MARGIN*2) / (MAX_LAT-MIN_LAT);
  107.  
  108.   float y = HEIGHT - (MARGIN + spanX * (longitude - MIN_LONG));
  109.   float x = MARGIN + spanY * (latitude - MIN_LAT);
  110.  
  111.   return new Point(x, y);
  112. }
  113.  
  114. class Point{
  115.   public float x, y;
  116.  
  117.   Point(float x, float y){
  118.     this.x = x;
  119.     this.y = y;
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement