Tarea 10

De Casiopea


TítuloDaniela Reyes: tarea 10
Tipo de ProyectoProyecto de Curso
Palabras Clavetarea 10
AsignaturaTaller Inicial 1ª y 2ª Etapa,
Del CursoImagen Escrita 2012,
CarrerasArquitectura
Alumno(s)Daniela Reyes
ProfesorHerbert Spencer

// Se agrega un atractor más, el que se complementa con el entregado en el ejemplo al mismo tiempo.



int MAX = 100;
Thing[] t = new Thing[MAX];
Attractor a;
attractor2 b;
boolean showVectors = false;
boolean velo = false;

void setup() {
  size(1024, 768);
  smooth();

  // Some random bodies
  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(width), random(height));
    t[i] = new Thing(ac, ve, lo, random(2, 100));
  }
  // Create an attractive body
  a = new Attractor(new PVector(width/2, height/2), 30, 1.4);
  b = new attractor2(new PVector(width/2, height/2), 12, 1.4);

  PFont font;
  font = createFont("Monaco", 18);
  textFont(font);
}

color cualquiera() {
  color c = color(random(255), random(255), random(255));
  return c;
}

void draw() {

  if (!velo)background(255); // fondo blanco

  a.rollover(mouseX, mouseY);
 b.rollover(mouseX, mouseY); // "escucha" al mouse para saber si hay rollover sobre el atractor

  a.go();
 b.go(); // * atractor: sea!

  for (int i = 0; i < t.length; i++) {

    // Calculate a force exerted by "attractor" on "thing"
    PVector f = a.calcGravForce(t[i]);
    PVector r = b.calcGravForce(t[i]);
    // Apply that force to the thing
    t[i].applyForce(f);
    t[i].applyForce(r);
    // Update and render
    t[i].go();
  }

  if (keyPressed) {
    if (key == 'a') {
      a.G += 0.1;
      b.G += 0.8;
    }
    if (key == 'z') {
      a.G -= 0.1;
      b.G -= 0.3;
    }
    if (key == 's') {
      a.mass += 5;
      b.mass += 46;
    }
    if (key == 'x') {
      a.mass -= 5;
      b.mass -= 1;
      a.mass = constrain(a.mass, 0, 100);
      b.mass = constrain(a.mass, 0, 150);
    }
  }
  fill(0);
  text("G = "+a.G+",\t la masa del atractor = "+a.mass, 30, height-30);
text("G2 = "+b.G+",\t la masa del attractor2 = "+b.mass, 520, height-30);

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


void mousePressed() {
  a.clicked(mouseX, mouseY);
  b.clicked(mouseX, mouseY);
}

void mouseReleased() {
  a.stopDragging();
  b.stopDragging();
}

void keyPressed() {
  if (key == ' ') {
    showVectors = !showVectors;
  }
  if (key == 'v') {
    velo = !velo;
  }
}

// Renders a vector object 'v' as an arrow and a location 'loc'
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, 128, 0, 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();
  }
}

**//------------------------Atractor 1-------------------------//**


// Attraction
// Daniel Shiffman <http://www.shiffman.net>

// A class for a draggable attractive body in our world

class Attractor2 {
  float mass;    // Mass, tied to size
  float G;       // Gravitational Constant
  PVector loc;   // Location
  boolean dragging = false; // Is the object being dragged?
  boolean rollover = false; // Is the mouse over the ellipse?
  PVector drag;  // holds the offset for when object is clicked on

  Attractor(PVector l_,float m_, float g_) {
    loc = l_.get();
    mass = m_;
    G = g_;
    drag = new PVector(0.0,0.0);
  }

  void go() {
    render();
    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, 250.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() {
    strokeWeight(mass / 10);
    stroke(0, 100);
    ellipseMode(CENTER);
    if (dragging) fill (50);
    else if (rollover) fill(100);
    else fill(175,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 = mouseY + drag.y;
    }
  }

}

**//---------------------------Atractor 2------------------------//**

class attractor2 {

 float mass;    // Mass, tied to size
 float G;       // Gravitational Constant
 PVector loc;   // Location
 boolean dragging = false; // Is the object being dragged?
 boolean rollover = false; // Is the mouse over the ellipse?
 PVector drag;  // holds the offset for when object is clicked on
 attractor2(PVector l_,float m_, float g_) {
   loc = l_.get();
   mass = m_;
   G = g_;
   drag = new PVector(0.0,0.0);
 }
 void go() {
   render();
   drag();
 }
 PVector calcGravForce(Thing t) {
   PVector dir = PVector.sub(loc,t.getLoc());       
   float d = dir.mag();                              
   d = constrain(d, 1.0, 50.0);                     
   dir.normalize();                                 
   float force = (G * d * t.getMass()) / (mass*mass);   
   dir.mult(force);                                  
   return dir;
 }
 // Method to display
 void render() {
   strokeWeight(mass / 10);
   stroke(0, 100);
   ellipseMode(CENTER);
   if (dragging) fill (50);
   else if (rollover) fill(100);
   else fill(175,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 = mouseY + drag.y;
   }
 }
}

**//---------------------------------------------------------//**


// Attraction
// Daniel Shiffman <http://www.shiffman.net>

// A class to describe a thing in our world, has vectors for location, velocity, and acceleration
// Also includes scalar values for mass, maximum velocity, and elasticity

class Thing {
  PVector loc; // location
  PVector vel; // velocity
  PVector acc; // acceleration
  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;
    col = color(random(180, 255), random(100, 175), random(30), 200);
  }
  
  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, 100);
    }    
  }

  // 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);
    // Multiplying by 0 sets the all the components to 0
    acc.mult(0);
  }
  
  // Method to display
  void render() {
    ellipseMode(CENTER);
    fill(col);
    ellipse(loc.x,loc.y,mass,mass);
    if (showVectors) {
      drawVector(vel,loc,20);
    }
  }
}