Isidora Correa Proyecto 10 IE 2012

De Casiopea




Títuloisidora correa tarea 10
Tipo de ProyectoProyecto de Curso
Palabras Clavetarea 10
Del CursoImagen Escrita 2012
CarrerasDiseño Gráfico, Diseño Industrial
Alumno(s)Isidora Correa
ProfesorHerbert Spencer

Ventana 1


int MAX = 40; Thing[] t = new Thing[MAX]; Attractor a; Attractor b; Attractor p; boolean showVectors = true; boolean velo = true;

void setup() {

 size(700,700);
 smooth();


 for (int i = 0; i < t.length; i++) {
   PVector ac = new PVector(0.0, 0.0);
   PVector ve = new PVector(random(-1, 1), random(-1, 1));
   PVector lo = new PVector(random(height), random(height));
   t[i] = new Thing(ac, ve, lo, random(2, 100));
 }
 
 a = new Attractor(new PVector(width/2, height/3), 70, 6.4);
 b = new Attractor(new PVector(width/8, height/2), 50, -6.2);
  p = new Attractor(new PVector(width/4, height/5), 10, 10);
 PFont font;
 font = createFont("trebuchet", 18);
 textFont(font);

}

color cualquiera() {

 color c = color(random(105), random(400), random(405));
 return c;

}

void draw() {

 if (!velo)background(255); // fondo blanco
 a.rollover(mouseY, mouseX);
 b.rollover(mouseX, mouseY); 
 p.rollover(mouseY, mouseY);
 a.go(25);
 p.go(#E2A0E5);
 b.go(#39E337); 
 for (int i = 0; i < t.length; i++) {


   PVector f = a.calcGravForce(t[i]);
   PVector g = b.calcGravForce(t[i]);
   PVector j = p.calcGravForce(t[i]);
   
   t[i].applyForce(f);
   t[i].applyForce(g);
   t[i].applyForce(j);
   
   t[i].go();
 }
 if (keyPressed) {
   if (key == 'a') {
     a.G += 0.1;
   }
   if (key == 'z') {
     a.G -= 0.1;
   }
   if (key == 's') {
     a.mass += 5;
   }
   if (key == 'x') {
     a.mass -= 5;
     a.mass = constrain(a.mass, 0, 100);
   }
 if (key == 'd') {
     b.G += 0.1;
   }
   if (key == 'c') {
     b.G -= 0.1;
   }
   if (key == 'f') {
     b.mass += 5;
   }
     if (key == 'v') {
     b.mass -= 5;
     b.mass = constrain(b.mass, 0, 100);
   
   } 
     if (key == 'g') {
     p.G += 0.1;
   }
   if (key == 'b') {
     p.G -= 0.1;
   }
   if (key == 'h') {
     p.mass += 5;
   }
   if (key == 'n') {
     p.mass -= 5;
     p.mass = constrain(p.mass, 0, 100);
   }
 }


 if (velo) {
   fill(255, 15);
   noStroke();
   rect(0, 0, width, height);
 }

}


void mousePressed() {

 a.clicked(mouseY, mouseY);
 b.clicked(mouseY, mouseY);
  p.clicked(mouseX, mouseY);

}

void mouseReleased() {

 a.stopDragging();
 b.stopDragging();
 p.stopDragging();

}

void keyPressed() {

 if (key == ' ') {
   showVectors = !showVectors;
 }
 if (key == 'q') {
   velo = !velo;
 }

}


void drawVector(PVector v, PVector loc, float scayl) {

 strokeWeight(.75);
 if (v.mag() > 0.0) {
   pushMatrix();
   float arrowsize = 10;
   // Translate to location to render vector
   translate(loc.x, loc.y);
   stroke(255, 140, 45, 200);
   // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
   rotate(v.heading2D());
   // Calculate length of vector & scale it to be bigger or smaller if necessary
   float len = v.mag()*scayl;
   // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
   line(0, 0, len, 0);
   line(len, 0, len-arrowsize, +arrowsize/2);
   line(len, 0, len-arrowsize, -arrowsize/2);
   popMatrix();
 }

}

Ventana 2

class Attractor {

 float mass;    
 float G;     
 PVector loc; 
 boolean dragging = false; 
 boolean rollover = false; 
 PVector drag;  
 Attractor(PVector l_,float m_, float g_) {
   loc = l_.get();
   mass = m_;
   G = g_;
   drag = new PVector(0.0,0.0);
 }
 void go(color colors) {
   render(colors);
   drag();
 }
 
 PVector calcGravForce(Thing t) {
   PVector dir = PVector.sub(loc,t.getLoc());        // Calculate direction of force
   float d = dir.mag();                              // Distance between objects
   d = constrain(d, 1.0, 260.0);                     // Limiting the distance to eliminate "extreme" results for very close or very far objects
   dir.normalize();                                  // Normalize vector (distance doesn't matter here, we just want this vector for direction)
   float force = (G * mass * t.getMass()) / (d * d); // Calculate gravitional force magnitude
   dir.mult(force);                                  // Get force vector --> magnitude * direction
   return dir;
 }
 // Method to display
 void render(color colorci) {
   strokeWeight(mass / 10);
   stroke(0, 100);
   ellipseMode(CENTER);
   if (dragging) fill (colorci,50);
   else if (rollover) fill(colorci,100);
   else fill(colorci,200);
   ellipse(loc.x,loc.y,mass*2,mass*2);
   noStroke();
 }
 
 
 // The methods below are for mouse interaction
 void clicked(int mx, int my) {
   float d = dist(mx,my,loc.x,loc.y);
   if (d < mass) {
     dragging = true;
     drag.x = loc.x-mx;
     drag.y = loc.y-my;
   }
 }
 void rollover(int mx, int my) {
   float d = dist(mx,my,loc.x,loc.y);
   if (d < mass) {
     rollover = true;
   } else {
     rollover = false;
   }
 }
 void stopDragging() {
   dragging = false;
 }
 

 void drag() {
   if (dragging) {
     loc.x = mouseX + drag.x;
     loc.y = mouseX + drag.x;
   }
 }

}

Ventana 3

class Thing {

 PVector loc; 
 PVector vel; 
 PVector acc;
 float mass;
 float max_vel;
 color col;
   
 Thing(PVector a, PVector v, PVector l, float m_) {
   acc = a.get();
   vel = v.get();
   loc = l.get();
   mass = m_;
   max_vel = 20.0;
   
 }
 
 PVector getLoc() {
   return loc;
 }
 PVector getVel() {
   return vel;
 }
 
 float getMass() {
   return mass;
 }
 void applyForce(PVector force) {
   force.div(mass);
   acc.add(force);
   if (showVectors) {
     drawVector(force,loc, 80);
   }    
 }
 // Main method to operate object
 void go() {
   update();
   render();
 }
 
 // Method to update location
 void update() {
   vel.add(acc);
   vel.limit(max_vel);
   loc.add(vel);
   
   acc.mult(1);
   col = color(0, 30*loc.mag(), 154, 160*vel.mag());
 }
 
 // Method to display
 void render() {
   ellipseMode(CENTER);
   fill(col);
   triangle(loc.x,loc.y,35,35,40,15);
   if (showVectors) {
     drawVector(vel,loc,20);
   }
 }

}