CSC220 Processing Sketch Examples

From dftwiki3
Revision as of 17:43, 10 November 2010 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


array2Lines

float[] x = new float[10];
float[] y = new float[10];
void setup() {
  size(500,500);
  background(0);
  for(int i = 0; i < x.length; i++) {
    x[i] = random(width);
    y[i] = random(height);
  }
}

void draw() {
  stroke(#0EAD7C);
  for(int i = 0; i < x.length-1; i++) {
    float x0 = x[i];
    float y0 = y[i];
    float x1 = x[i+1];
    float y1 = y[i+1];
    line(x0,y0,x1,y1);
  }
}

Basic Forms

/*
 processing bootstrap
 init global vars
 call setup
 call draw (at fixed fps)
*/

float r = 50;

void setup() {
  size(500, 300); // window size
  background(150,50,50);
  smooth();
  y = height/2;
}

float x = 0;
float y = 0;

void draw() {
  background(150,50,50);
  strokeWeight(5);
  stroke(50,50, 150);
  
  float d = dist(x,y,mouseX,mouseY);
  if((d < r) && mousePressed) {
    fill(0,255,0);
  } else {
    fill(250,0,0);
  }
  ellipse(x, y, r*2, r*2);
  x++;
  x= (x > width + r) ?  - r : x;
  
  
  
  stroke(0);
  line(0, height/2.0 + r, width, height/2.0-r);
  noStroke(); 
  fill(#C1631B) ;
  rect(10,10, 80,20);
  noFill();
  stroke(#C1631B);
  arc(width/2.0, height, width, height * 5/3, TWO_PI - PI * 3.0/4.0, TWO_PI - PI * (1/8.0));
  
}

void mouseClicked() {
}
void mouseMoved() {
}
void mouseDragged() {
}
void mouseReleased(){
}
void mousePressed() {
}

GPS Trace


// globals
float minLat = MAX_FLOAT;
float minLong = MAX_FLOAT;
float maxLat = MIN_FLOAT;
float maxLong = MIN_FLOAT;

int coordCnt = 0;
float[] longitudes = new float[25];
float[] latitudes  = new float[longitudes.length];


void setup() {

  BufferedReader reader = createReader("walk.csv");  
  try {
    String ln = reader.readLine();
    while(ln != null) {
      // enlarge arrays if needed
      if(coordCnt >= longitudes.length) {
        longitudes = expand(longitudes);
        latitudes = expand(latitudes);
      }
      String[] coords = split(ln,',');
      float longitude = float(coords[0]); //x
      float latitude = float(coords[1]); //y

      minLat = (minLat < latitude) ? minLat : latitude;
      minLong = (minLong < longitude) ? minLong : longitude;
      maxLat = (maxLat > latitude) ? maxLat : latitude;
      maxLong = (maxLong > longitude) ? maxLong : longitude;


      longitudes[coordCnt] = longitude; // y
      latitudes[coordCnt] = latitude; // y
      coordCnt++;
      ln = reader.readLine();
    }
  } 
  catch (IOException e) {
  }

  size(500,500);
  smooth();
  //  noLoop();
}

void draw() {
  background(100);

  int minI = -1;
  float minX = -1;
  float minY = -1;
  float minD = MAX_FLOAT;


  for(int i = 0; i < coordCnt-1; i++) {
    float x0 = map(longitudes[i], minLong, maxLong, 10, width-10);
    float y0 = map(latitudes[i], minLat, maxLat, 10, height-10);
    float x1 = map(longitudes[i+1], minLong, maxLong, 10, width-10);
    float y1 = map(latitudes[i+1], minLat, maxLat, 10, height-10);
    stroke(0,0,0);
    strokeWeight(1);
    line(x0,y0, x1,y1);
    stroke(255,0,0,100);
    strokeWeight(5);
    point(x0,y0);

    float d = dist(x0, y0, mouseX, mouseY);
    if(d < 15) {
      if(d < minD) {
        minX = x0;
        minY = y0;
        minD = d;
        minI = i;
      }
      strokeWeight(15);
      point(x0,y0);
    }
  }
  if(minD < 5) {
    stroke(0);
    strokeWeight(3);
    point(minX,minY);
    fill(50);
    text(longitudes[minI] + ", " +latitudes[minI], minX+10,minY+10);
  }
}

mouseInteraction


// globals
float minLat = MAX_FLOAT;
float minLong = MAX_FLOAT;
float maxLat = MIN_FLOAT;
float maxLong = MIN_FLOAT;

int coordCnt = 0;
float[] longitudes = new float[25];
float[] latitudes  = new float[longitudes.length];


void setup() {

  BufferedReader reader = createReader("walk.csv");  
  try {
    String ln = reader.readLine();
    while(ln != null) {
      // enlarge arrays if needed
      if(coordCnt >= longitudes.length) {
        longitudes = expand(longitudes);
        latitudes = expand(latitudes);
      }
      String[] coords = split(ln,',');
      float longitude = float(coords[0]); //x
      float latitude = float(coords[1]); //y

      minLat = (minLat < latitude) ? minLat : latitude;
      minLong = (minLong < longitude) ? minLong : longitude;
      maxLat = (maxLat > latitude) ? maxLat : latitude;
      maxLong = (maxLong > longitude) ? maxLong : longitude;


      longitudes[coordCnt] = longitude; // y
      latitudes[coordCnt] = latitude; // y
      coordCnt++;
      ln = reader.readLine();
    }
  } 
  catch (IOException e) {
  }

  size(500,500);
  smooth();
  //  noLoop();
}

void draw() {
  background(100);

  int minI = -1;
  float minX = -1;
  float minY = -1;
  float minD = MAX_FLOAT;


  for(int i = 0; i < coordCnt-1; i++) {
    float x0 = map(longitudes[i], minLong, maxLong, 10, width-10);
    float y0 = map(latitudes[i], minLat, maxLat, 10, height-10);
    float x1 = map(longitudes[i+1], minLong, maxLong, 10, width-10);
    float y1 = map(latitudes[i+1], minLat, maxLat, 10, height-10);
    stroke(0,0,0);
    strokeWeight(1);
    line(x0,y0, x1,y1);
    stroke(255,0,0,100);
    strokeWeight(5);
    point(x0,y0);

    float d = dist(x0, y0, mouseX, mouseY);
    if(d < 15) {
      if(d < minD) {
        minX = x0;
        minY = y0;
        minD = d;
        minI = i;
      }
      strokeWeight(15);
      point(x0,y0);
    }
  }
  if(minD < 5) {
    stroke(0);
    strokeWeight(3);
    point(minX,minY);
    fill(50);
    text(longitudes[minI] + ", " +latitudes[minI], minX+10,minY+10);
  }
}