Luciano Morales Saavedra ,tarea 10

De Casiopea


TítuloLuciano Morales Saavedra : tarea 10
Tipo de ProyectoProyecto de Curso
Palabras Clavetarea 10
Período2012-
AsignaturaImagen Escrita 2012,
Del CursoImagen Escrita 2012,
CarrerasDiseño Gráfico"Diseño Gráfico" is not in the list (Arquitectura, Diseño, Magíster, Otra) of allowed values for the "Carreras Relacionadas" property., Diseño Industrial"Diseño Industrial" is not in the list (Arquitectura, Diseño, Magíster, Otra) of allowed values for the "Carreras Relacionadas" property.
Alumno(s)Luciano Morales
ProfesorHerbert Spencer

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 = 5;         
  G = 30;     
  drag = new PVector(4.0,-7) ;
}
void go() {
  render();
  drag();
}
PVector calcGravForce(Thing t) {
  PVector dir = PVector.sub(loc,t.getLoc());        
  float d = dir.mag();                              
  d = constrain(d, 1.0, 300.0);                     
  dir.normalize();                                  
  float force = (G * mass * t.getMass()) / (d * d); 
  dir.mult(force);                                  
  return dir;
}
void render() {
  strokeWeight(mass / 50);
  stroke(0, 100);
  ellipseMode(CENTER);
  if (dragging) fill (100);
  else if (rollover) fill(50);
  else fill(175,200);
  ellipse(loc.x,loc.y,mass*2,mass*2);
  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 = 30.0;
  col = color(random(#FFFF00, #DF0101), random(#08298A, #08088A), random(50), 55);
}

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);
  }    
}
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,0);            
  }
}

}


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

void setup() {

background (555);
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(0, 0), random(0, 1));
  PVector lo = new PVector(random(width), random(height));
  t[i] = new Thing(ac, ve, lo, random(6, 67));
}

a = new Attractor(new PVector(width/2, height/2), 38, 1.4);
PFont font;
font = createFont("arial", 10);
textFont(font);

}

color cualquiera() {

color c = color(random(255), random(255), random(255));
return c;

}

void draw() {

if (!velo)background(255); 
a.rollover(mouseX-9, mouseY-9
); 
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 == 'b') {
    a.G -= 0.1;
  }
  if (key == 'c') {
    a.mass += 5;
  }
  if (key == 'd') {
    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, 15);
  noStroke();
  rect(0, 0, width, height);
}

}


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(.75);
if (v.mag() > 0.0) {
  pushMatrix();
  float arrowsize = 10;
  translate(loc.x, loc.y);
  stroke(255, 128, 0, 200);
  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();
}

}