Leninn G 1° ARQ 2012

De Casiopea


Títulot10
Tipo de ProyectoProyecto de Taller
Palabras Clavetarea 10
Período2012-2012
AsignaturaImagen Escrita 2012,
Del CursoImagen Escrita 2012,
CarrerasArquitectura
Alumno(s)Leninn Gustainsson
ProfesorHerbert Spencer


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

void setup() {

size(700, 500);
smooth();
stroke(#ff8832,#21bb56);
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.02, 0.07));
  PVector lo = new PVector(random(width/5), random(height/8));
  t[i] = new Thing(ac, ve, lo, random(6, 67));
}

a = new Attractor(new PVector(width/6, height/6), 60, 0.6);
PFont font;
font = createFont("Monaco", 13);
textFont(font);

}

color cualquiera() {

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

}

void draw() {

if (!velo)background(255); 
a.rollover(mouseX-3, mouseY-3); 
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 == 'q') {
    a.G += 3;
  }
  if (key == 'w') {
    a.G -= 1;
  }
  if (key == 'e') {
    a.mass += 3;
  }
  if (key == 'r') {
    a.mass -= 0;
    a.mass = constrain(a.mass, 0, 300);
  }
}
fill(#88bb21);
text("G = "+a.G+",\t la masa del atractor = "+a.mass, 80, height-30);

}


void mousePressed() {

a.clicked(mouseX, mouseY);

}

void mouseReleased() {

a.stopDragging();

}

void keyPressed() {

if (key == 'a') {
  showVectors = !showVectors;

}

}


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

strokeWeight(0.009);
if (v.mag() > 0.0) {
  pushMatrix();
  float arrowsize = 10;
  
  translate(loc.x, loc.y);
  stroke(125, 8, 109, 340);
  
  rotate(v.heading2D());
  
  float len = v.mag()*scayl;
  
  line(0, 10, 1, len);
  line(len, 0, arrowsize, arrowsize/2);
  line(len, 0, -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(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, 50.0);                     
  dir.normalize();                                  
  float force = (G * mass * t.getMass()) / (d * d); 
  dir.mult(force);                                  
  return dir;
}
void render() {
  strokeWeight(mass / 20);
  stroke(#ffff00, #3d85c6);
  ellipseMode(CENTER);
  if (dragging) fill (100);
  else if (rollover) fill(100);
  else fill(175,20);
  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 = 20.0;
  col = color(random(#007cbb, 2557), random(#ffff00, 2222), random(24),25);
}

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