// MUTUAL ATTRACTION VISUALIZATION // Mark Roland // Oct 29, 2005 // ALL OF THIS CODE WAS BORROWED FROM DANIEL SHIFFMAN @ // http://www.shiffman.net/itp/classes/nature/week02/ // Only the visualization has been changed by me //establish an array of MAX Things final int MAX = 3; Thing[] t = new Thing[MAX]; boolean showVectors = false; int draw_mode = 1; int color_mode = 1; void setup() { size(500, 250); background(0); reset(); smooth(); } void draw() { for (int i = 0; i < t.length; i++) { //for every Thing t[i] for (int j = 0; j < t.length; j++) { //for every Thing t[j] if (i != j) { //make sure we are not calculating gravtional pull on oneself Vector2D f = t[i].calcGravForce(t[j]); //use the function we wrote above t[i].add_force(f); //add the force to the object to affect its acceleration } } t[i].go(); //implement the rest of the object's functionality } } /*A reset function that initializes all the objects*/ void reset() { noStroke(); fill(255); rect(0,0,width,height); for (int i = 0; i < t.length; i++) { Vector2D ac = new Vector2D(0.0,0.0); Vector2D ve = new Vector2D(0.0,0.0); Vector2D lo = new Vector2D(random(.1*width, .9*width),random(.1*height, .9*height)); t[i] = new Thing(ac,ve,lo,random(5,10),0.2); } key = 'a'; } void mousePressed() { reset(); } public void drawVector(Vector2D vel, Vector2D loc, float scayl) { pushMatrix(); float arrowsize = 10; //translate to location to render vector translate(loc.x(),loc.y()); stroke(255); //call vector heading function to get direction (note that pointing up is a heading of 0) and rotate rotate(vel.heading()); //calculate length of vector & scale it to be bigger or smaller if necessary float len = vel.magnitude()*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(); }