Constanza Arancibia Tarea 7

De Casiopea
Constanza Arancibia Tarea 7


TítuloConstanza Arancibia Tarea 7
Palabras Clavetarea 7
AsignaturaImagen Escrita 2012,
Del CursoImagen Escrita 2012,
CarrerasArquitectura
Alumno(s)Constanza Arancibia
 
import processing.pdf.*;
boolean saveOneFrame = false;
Insect[] ins;
float margin = 70;
void setup() {
  size(1050, 700);
  int ynum = 11;
  int xnum = 9;

  ins = new Insect[ynum * xnum];

  float ysp = (height - (2 * margin)) / ((float)ynum - 1);
  float xsp = (width - (2 * margin)) / ((float)xnum - 1);

  int c = 0; // counter

  for (float y = margin; y <= height - margin; y+= ysp) {
    for (float x = margin; x <= width - margin; x += xsp) {
      ins[c] = new Insect(x, y);
      c++;
    }
  } 
  smooth();
}


void draw() {
  if (saveOneFrame == true) {  // When the saveOneFrame boolean is true,
beginRecord(PDF, "insects-####.pdf");  // start recording to the PDF
}
  background(255);
  for (int i = 0; i < ins.length; i++) {
    ins[i].render();
  }
  if (saveOneFrame == true) {  // If the PDF has been recording,
endRecord();  // stop recording,
saveOneFrame = false;  // and set the boolean value to false
}
}


class Insect {
  float x, y;
  float[][] v; // vertices
  int vn;  // número aleatorio de vértices
  float tam;  // tamaño
  float w, h; // width, height

  Insect(float x, float y) {
    this.x = x;
    this.y = y;
    vn = round(random(10, 40));
    v = new float[vn][2];
    tam = 40;
    init();
  }

  void init() {
    w = tam/2;
    h = tam;
    for (int i = 0; i < vn; i++) {
      v[i][0] = random(w);
      v[i][1] = random(-h/2, h/2);
    }
  }

  void trace() {
    
   fill(random(255),random(255),random(255)); //el insecto cambia de color
   stroke(0);
    strokeWeight(0.25);
    beginShape();
    vertex(v[0][0], v[0][1]);
     for (int i = 0; i < vn/2; i++) {
      curveVertex(v[i][0], v[i][1]); //parte que no se mueve
    }
    for (int i = vn/2; i < vn; i++) {
    bezierVertex(20,random(40),20,random(40),v[vn-1][0], v[vn-1][1]); //parte en movimiento del insecto
    }
    endShape();
  }

  void render() {
    pushMatrix();
    {
      translate(x, y);
      trace();
      scale(-1, 1);
      trace();
    }
    popMatrix();
  }
}

void keyPressed() {
  if (key == ' ') {
    for (int i = 0; i < ins.length; i++) {
      ins[i].init();
      
    }
   
  }
  if (key == 'a') {
    for (int i = 0; i < ins.length; i++) {
      ins[i].tam++;
      ins[i].init();
    }
  }
  if (key == 'z') {
    for (int i = 0; i < ins.length; i++) {
      ins[i].tam--;
      ins[i].init();
    }
  }
  if(key=='p'){
     saveFrame("insects.jpg"); // grabar imagen
  }
   
}
void mousePressed() {  // When a mouse button is pressed,
saveOneFrame = true;  // trigger PDF recording within the draw()
}