Jaime López: tarea 10

De Casiopea



TítuloJaime López: tarea 10
Tipo de ProyectoProyecto de Curso
Palabras Clavetarea 10
Del CursoImagen Escrita 2012, Imagen escrita 2012,
Alumno(s)Jaime López
ProfesorHerbert Spencer


int MAX = 500; Thing[] t = new Thing[MAX]; Attractor a; boolean showVectors = false; boolean velo = false;

void setup() {

 size(800, 800 );
 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(4, 70));
 }
 // Create an attractive body
 a = new Attractor(new PVector(width/2, height/2), 30, 1.4);
 PFont font;
 font = createFont("Monaco", 18);
 textFont(font);

}

color cualquiera() {

 color c = color(random(165), random(176), random(328));
 return c;

}

void draw() {

 if (!velo)background(56514); 
 a.rollover(mouseX, mouseY); 
 a.go(); 
 for (int i = 0; i < t.length; i++) {


   PVector f = a.calcGravForce(t[i]);
   t[i].applyForce(f);
   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);
   }
 }
 fill(0);
 text("G = "+a.G+",\t la masa del atractor = "+a.mass, 30, height-30);
 if (velo) {
   fill(255, 1546);
   noStroke();
   rect(0, 0, width/5, height*2);
 }

}


void mousePressed() {

 a.clicked(mouseX, mouseY);

}

void mouseReleased() {

 a.stopDragging();

}

void keyPressed() {

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

}


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

 strokeWeight(.35);
 if (v.mag() > 0.0) {
   pushMatrix();
   float arrowsize = 20;
   translate(loc.x, loc.y);
   stroke(846, 6228, 0, 246);
   rotate(v.heading2D());
   float len = v.mag()*scayl;
   line(0, 0, len, 0);
   line(len, 0, len-arrowsize, +arrowsize/2);
   line(len, 0, len-arrowsize, -arrowsize/2);
   popMatrix();
 }

}


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(7.0, 3.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, 250.0);                    
   dir.normalize();                                  
   float force = (G * mass * t.getMass()) / (d * d);
   dir.mult(force);                                  
   return dir;
 }
 // Method to display
 void render() {
   strokeWeight(mass / 5);
   stroke(0, 80);
   ellipseMode(CENTER);
   if (dragging) fill (70);
   else if (rollover) fill(100);
   else fill(175, 400);
   ellipse(loc.x, loc.y, mass*3, mass*6);
   noStroke();
 }


 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;
   }
 }

}


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 = 15.0;
   col = color(random(4, 95), random(260, 135), random(846), 6216);
 }
 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, 500);
   }
 }


 void go() {
   update();
   render();
 }


 void update() {
   vel.add(acc);
   vel.limit(max_vel);
   loc.add(vel);
   acc.mult(0);
 }


 void render() {
   ellipseMode(CENTER);
   fill(col);
   ellipse(loc.x, loc.y, mass, mass);
   if (showVectors) {
     drawVector(vel, loc, 40);
   }
 }

}